binary code of 2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

binary code of 2

Can anybody tell me, why 0b010 and 0b10 both are valid binary codes to represent the integer 2? I stumbled on it when I experimented with the binary AND operator '&'.

30th May 2019, 4:46 PM
thefooture
2 Answers
+ 9
“0b” marks the start of the binary (b= binary), but it is not the value itself it is just one representation for binary system. This is standing on the almost left side of the representation. On the right hand is 010 or 10 or 00000010. The digits have a value each starting from right to left : 128 64 32 16 8 4 2 1 0 0 0 0 0 0 1 0 Every time there is a 1 in the bottom line the value in the top line is used. 128x0 + 64x0 +32x0 + 16x0 + 8x0 + 4x0 + 2x1 + 1x0 = 2.
30th May 2019, 5:12 PM
Lothar
Lothar - avatar
0
Many thanks Lothar. I find your explanation very helpful.
30th May 2019, 5:37 PM
thefooture