## DEV Community π©βπ»π¨βπ» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Posted on

# Working with Binary like Decimal

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

1. Every number except zero has a `1` ready state in its most significant bit, since every number contains a 20.

2. The number of zeros `0` that follow the `1` 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
``````
3. The number of bits in a 2n number is `n + 1`, because the possible outcome of `n` number of bits ranges from 0 to 2n-1, not 1 to 2n, for all unsigned integers.

# Decimal to Binary β Conversion

1. Let's pick some digits to make a decimal integer, say `13`.

2. From our table, let's pick the closest number to this, `8`, 23 = 10002.

3. We have a variable `temp = 1000`, in binary. The remainder is `5`.

4. The closest number to `5` from the table is `4`, 1002.

5. Since the number is `5`, we could simply say the remainder is `1` 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.
``````
6. So we add `5 = 101` to `8 = 1000` and we get `13 = 1101`

# Binary to Decimal β Conversion

1. Let's pick some bits to make a binary integer, say `10010011`.

2. 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`

3. 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