What would the code look like to convert a positive decimal (base 10 integer) to its binary form (base 2)?

I am tasked with developing a code that will convert a integer to binary form and display the result. I cannot get my code to work. What would the code look like to perform such an action?

Answers (3)

You need to use dec2base command. To convert 23 decimal number to its base 2 representation, type
dec2base(23,2)
>> dec2bin(123)
ans = 1111011
If you want DIY code, then you will need mod and a loop.
Examine:
TheInt = 1736;
bitget(uint32(TheInt), 32:-1:1)
Discard leading 0 values if you want.

Categories

Asked:

on 17 Jan 2018

Answered:

on 17 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!