Binary, hexadecimal and octal integers are the most common number bases generally used in computer science and computer science related fields.
Have you tried debugging a compiled C program, you'd either be looking at plain hexadecimals or Assembly instructions alongside the hexadecimal equivalent. Some people are so technical-savvy that they know what they are doing by just staring at hexadecimal, alternatively called an hexdump. Some will only understand what the hexadecimal is doing when they see the equivalent Assembly code. I won't lie, either way some don't know what is happeningβAssembly is crazy it can make you too ππ.
The fun fact is; you don't need to debug a compiled C program or do reverse engineering before you need to understand these three beautiful number bases. You could just be a random programmer working Binary Manipulation like bit masks, shifting, turning on and off bits, to come to the realization that you need to know how these number systems work.
Binary
Binary integers like other digits are written from left to rightβthe most significant bit to least significant bit.
10001011 -> 139
^ ^
^ ^
^ least significant bit
most significant bit
I converted that binary number to it's decimal, in less than 10sec, just as I was writing, merely looking at it
The trick
You should know this if you've ever worked with binary numbers
Factor | Decimal | Binary |
---|---|---|
2^{0} | 1 | 1 |
2^{1} | 2 | 10 |
2^{2} | 4 | 100 |
2^{3} | 8 | 1000 |
2^{4} | 16 | 10000 |
2^{5} | 32 | 100000 |
2^{6} | 64 | 1000000 |
2^{7} | 128 | 10000000 |
2^{8} | 256 | 100000000 |
2^{9} | 512 | 1000000000 |
2^{10} | 1024 | 10000000000 |
2^{11} | 2048 | 100000000000 |
2^{12} | 4096 | 1000000000000 |
The power of 0 - 8
(1bit - 9bits) that is, 1 - 256
, may be enough for you to know. At least for basic binary calculations.
I guess you have noticed a pattern already. Yes, let's assume 2^{0} as ground state. Every binary integer, that has at least a bit turned on, has this in them. So there is a 2^{0} in 2, 4, 8, ..., 2^{n}.
The pattern
Every number except zero has a
1
ready state in its most significant bit, since every number contains a 2^{0}.-
The number of zeros
0
that follow the1
from the most significant bit, is the power of 2. So 2^{1} = 10_{2} = 2.
The 1 from the most significant bit is owned by 2^0 The following 0 is accounted for, by the power on 2
The number of bits in a 2^{n} number is
n + 1
, because the possible outcome ofn
number of bits ranges from 0 to 2^{n}-1, not 1 to 2^{n}, for all unsigned integers.
Decimal to Binary β Conversion
Let's pick some digits to make a decimal integer, say
13
.From our table, let's pick the closest number to this,
8
, 2^{3} = 1000_{2}.We have a variable
temp = 1000
, in binary. The remainder is5
.The closest number to
5
from the table is4
, 100_{2}.-
Since the number is
5
, we could simply say the remainder is1
and add it to 100_{2}, which gives 101_{2}.
Alternatively, you could turn on the first turned-off bit from the least significant bit (rightmost side), and turn off the bit to the right of the one you turned on, if there is any.
10000 = 16 To add 1, turn on the rightmost 0, that is change it to 1 To give 10001 = 17 10001 = 17 To add 1, turn on the rightmost 0, and turn off the 1 closest to the 0 you turned on. To give 10010 = 18 See the pattern: 10000 = 16 10 = 2 10000 + 10 = 10010 = 18.
So we add
5 = 101
to8 = 1000
and we get13 = 1101
Binary to Decimal β Conversion
Let's pick some bits to make a binary integer, say
10010011
.Break the bits from the rightmost side, and replace whichever bit you have taken with
0
in the original number
11 = 3
,remainder = 10010000
10000 = 16
,remainder = 10000000
10000000 = 128
,remainder = 0
Add all: 128 + 16 + 3 = 147.
Something you need to know again, is the odd end of n
number of bits, that is the 2^{n}-1. Here is another table for that.
Factor - 1 | Decimal | Binary |
---|---|---|
2^{1} - 1 | 1 | 1 |
2^{2} - 1 | 3 | 11 |
2^{3} - 1 | 7 | 111 |
2^{4} - 1 | 15 | 1111 |
2^{5} - 1 | 31 | 11111 |
2^{6} - 1 | 63 | 111111 |
2^{7} - 1 | 127 | 1111111 |
2^{8} - 1 | 255 | 11111111 |
2^{9} - 1 | 511 | 111111111 |
2^{10} - 1 | 1023 | 1111111111 |
2^{11} - 1 | 2047 | 11111111111 |
2^{12} - 1 | 4095 | 111111111111 |
At least you should know this from 1 - 255.
I hope you enjoyed this π. You can fav it here and follow me on twitter
Top comments (0)