Reputation: 11
In my project i want to convert binary bits into array .
For example :
binary value of
a= dec2bin(1) = 0001
but i want to convert it into array and store like this
a=[0 0 0 1]
Upvotes: 1
Views: 1826
Reputation: 4551
You can use bitand
, e.g.,
>> bitand(1, 2.^(7:-1:0)) > 0
ans =
0 0 0 0 0 0 0 1
Or
bitand(10, 2.^(7:-1:0)) > 0
ans =
0 0 0 0 1 0 1 0
And, if you need to calculate many powers of two to include for an arbitrary number, you can use ceil(log2(theNumber))
Upvotes: 0
Reputation: 74940
Use str2num
of the transposed array a
:
a = dec2bin(1,4);
out = str2num(a')';
This way, each element of the string a
is individually converted into a number.
Upvotes: 2