Converting a Integer to Binary
Let’s take a number:
78
and convert it to binary. We know an 8 bit number is between 0 and 255, let’s draw this out and use 0’s initially, where the top row represents the integer equivalent:
POS 1 2 3 4 5 6 7 8
INT 128 64 32 16 8 4 2 1
———————————————————-
BIN 0 0 0 0 0 0 0 0
Let’s convert 78 to decimal now:
78 | 0100 0000 | 78 – “64” = 14
78 | 0100 1000 | 14 – “8” = 6
78 | 0100 1100 | 6 – “4” = 2
78 | 0100 1110 | 2 – “2” = 0
So, binary representation of 78 is “0100 1110” . Now, how do we put this into logic? Let’s try % and see what happens with the number as we progressively convert it, because modulus (%) returns either a 0 or 1 which is exactly what we want. 🙂
78 % 2 = 0 # Let’s try modulus, is either gives 0 or 1, ideal for this. No remainder! Let’s store this 0 in position 1 | 0000 0000
78 / 2 = 39 # Now devide by 2 to get the next number we need to work with. NOTE: / 2 results in a truncated value so we
use that to our advantage.
39 % 2 = 1 # Modulus again. Now we get 1. Store this in the next bit position. | 0100 0000
39 / 2 = 19 # Divide by 2 now. Now we get 19 2 x 19 is 38 but we already captured the 1 above. 😉
19 % 2 = 1 # Modulus again. Now we get 1. Store it again | 0110 0000
19 / 2 = 9 # Divide by 2 again.
9 % 2 = 1 # Modulus again. Now we get 1. Store it again. | 0111 0000
9 / 2 = 4 # Divide by 2 again.
4 % 2 = 0 # Modulus again. Now we get 0. Store it again. | 0111 0000
4 / 2 = 2 # Divide by 2 again.
2 % 2 = 0 # Modulus again. Now we get 0. Store it again. | 0111 0000
2 / 2 = 1 # Divide by 2 again.
1 % 2 = 1 # … | 0111 0010
1 / 2 = 0 #
0 % 2 = 0 # … | 0111 0010
You can see above is like a for loop.
So our answer after the above is 0111 0010. But 0111 0010 != 0100 1110. So what’s going on! It’s backwards. Let’s rewrite 0111 0010 backwards:
0100 1110
and now it matches our earlier answer, which is correct.
—————————————————————————————————————————————————————-
Second way to do this is using the >> and & C operators. The >> operator, will move all the bits representing 78 to the right. For example, if 78 is 0100 1110, then:
[A] 0100 1110
>> # Shift to right.
[B] 0010 0111
>> # and again and so fourth….
Now let’s take [A] and & it with 1. 1 is represented as 0000 0001, so:
0100 1110 # = 78
&
0000 0001
———-
0000 0000 # Digit 1 is 0
>>
0010 0111 # 32 + 4 + 2 + 1 = 39
&
0000 0001
———-
0000 0001 # Digit 2 is 1
>>
0001 0011 # 16 + 2 + 1 = 19
&
0000 0001
———-
0000 0001 # Digit 3 is 1
>>
0000 1001 # * + 1 = 9
&
0000 0001
———-
0000 0001 # Digit 4 is 1
>>
0000 0100 # 4 = 4
&
0000 0001
———-
0000 0000 # Digit 5 is 0
>>
0000 0010 # 2 = 2
&
0000 0001
———-
0000 0000 # Digit 6 is 0
>>
0000 0001 # 1 = 1
&
0000 0001
———-
0000 0000 # Digit 7 is 1
>>
0000 0000 # 0 = 0
&
0000 0001
———-
0000 0001 # Digit 8 is 0
>>
0000 0000 # IF the binary representation = 0 in decimal, we stop the program here.
Great! Now lets what we get when we combine all “Digit N is X” from above:
01110010
again, this is backwards, so rewriting it:
0100 1110
Gives us the answer.