Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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]
Use str2num of the transposed array a:
str2num
a
a = dec2bin(1,4); out = str2num(a')';
This way, each element of the string a is individually converted into a number.
Add a comment
You can use bitand, e.g.,
bitand
>> 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))
ceil(log2(theNumber))
Required, but never shown
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.
Explore related questions
See similar questions with these tags.