Conversion of number to binary

how are numbers converted to their binary equivalent ?

solution1:
Write a program following the below logic:

1.divide the number with 2 and store the remainder of the division i.e. either 0(if number is even) or 1(if number is odd ).

2.replace the number with its quotient.

3.perform step 1 and 2 till the quotient is greater than 0.

example:
num=25;
num%2=1; //1
num=num/2; //12
num%2=0; //01
num=num/2; //6
num%2=0 // 001
num=num/2; //3;
num%2=1; //1001
num=num/2; //1
num%2=1; //11001
num=num/2; //0
loop terminates

solution 2:
You can use standard library “bitset” to convert the number to its binary equivalent

// default constructor initializes with all bits 0
bitset<6> bset1;

// bset2 is initialized with bits of 20
bitset<8> bset2(20);

// bset3 is initialized with bits of specified binary string
bitset<5> bset3(string(“1100”));

cout << bset1; // 000000
cout << bset2; // 00010100
cout << bset3l; // 01100

hope this would help. if you still have doubts, feel free to ask.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.