Faster way for decimal to binary conversion
<p>Considering an array of integer numbers: </p>

<pre><code>X = [1, 3, 3, 3,2,3 ,1,1,2,3]
</code></pre>

<p>I want to convert each number to its 4 bit binary equivalent. I have done the following, but the result is not correct. It seems that the MSB and LSB are reversed. I tried using MATLAB's function with <code>decimal_to_bin =dec2bin(X)</code> but even this output is wrong.</p>

<p>How can I fix this?</p>

<pre><code>X = [1, 3, 3, 3,2,3 ,1,1,2,3];
b = 4;
lookup_table = generate_lookupTable(b);
BinaryX = lookup_table(X,:);



function result = generate_lookupTable(b)

% generate binary code

k = 2^b;
result = zeros(k,b);

for i=1:k
    for j=1:b
        result(i,j) = bitand(uint8(2^(j-1)),uint8(i-1))/uint8(2^(j-1));
    end
end

end
</code></pre>

<p>The output that I got is <code>BinaryX</code></p>

<pre><code>0   0   0   0
0   1   0   0
0   1   0   0
0   1   0   0
1   0   0   0
0   1   0   0
0   0   0   0
0   0   0   0
1   0   0   0
0   1   0   0
</code></pre>

<p><a href="https://i.stack.imgur.com/36R4L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/36R4L.png" alt="screenshot"></a></p>