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 |
---|---|---|
20 | 1 | 1 |
21 | 2 | 10 |
22 | 4 | 100 |
23 | 8 | 1000 |
24 | 16 | 10000 |
25 | 32 | 100000 |
26 | 64 | 1000000 |
27 | 128 | 10000000 |
28 | 256 | 100000000 |
29 | 512 | 1000000000 |
210 | 1024 | 10000000000 |
211 | 2048 | 100000000000 |
212 | 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 20 as ground state. Every binary integer, that has at least a bit turned on, has this in them. So there is a 20 in 2, 4, 8, ..., 2n.
The pattern
Every number except zero has a
1
ready state in its most significant bit, since every number contains a 20.-
The number of zeros
0
that follow the1
from the most significant bit, is the power of 2. So 21 = 102 = 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 2n number is
n + 1
, because the possible outcome ofn
number of bits ranges from 0 to 2n-1, not 1 to 2n, 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
, 23 = 10002.We have a variable
temp = 1000
, in binary. The remainder is5
.The closest number to
5
from the table is4
, 1002.-
Since the number is
5
, we could simply say the remainder is1
and add it to 1002, which gives 1012.
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 2n-1. Here is another table for that.
Factor - 1 | Decimal | Binary |
---|---|---|
21 - 1 | 1 | 1 |
22 - 1 | 3 | 11 |
23 - 1 | 7 | 111 |
24 - 1 | 15 | 1111 |
25 - 1 | 31 | 11111 |
26 - 1 | 63 | 111111 |
27 - 1 | 127 | 1111111 |
28 - 1 | 255 | 11111111 |
29 - 1 | 511 | 111111111 |
210 - 1 | 1023 | 1111111111 |
211 - 1 | 2047 | 11111111111 |
212 - 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)