1

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]

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.