Working with Binary like Decimal
Tags: devops, coding, tutorial
Published: 2020-01-03 06:37:36

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][bit-m] 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

<table>
<tr>
<th>Factor</th>
<th>Decimal</th>
<th>Binary</th>
</tr>

<tr>
<td>
2<sup>0</sup>
</td>
<td>1</td>
<td>1</td>
</tr>

<tr>
<td>
2<sup>1</sup>
</td>
<td>2</td>
<td>10</td>
</tr>

<tr>
<td>
2<sup>2</sup>
</td>
<td>4</td>
<td>100</td>
</tr>

<tr>
<td>
2<sup>3</sup>
</td>
<td>8</td>
<td>1000</td>
</tr>

<tr>
<td>
2<sup>4</sup>
</td>
<td>16</td>
<td>10000</td>
</tr>

<tr>
<td>
2<sup>5</sup>
</td>
<td>32</td>
<td>100000</td>
</tr>

<tr>
<td>
2<sup>6</sup>
</td>
<td>64</td>
<td>1000000</td>
</tr>

<tr>
<td>
2<sup>7</sup>
</td>
<td>128</td>
<td>10000000</td>
</tr>

<tr>
<td>
2<sup>8</sup>
</td>
<td>256</td>
<td>100000000</td>
</tr>

<tr>
<td>
2<sup>9</sup>
</td>
<td>512</td>
<td>1000000000</td>
</tr>

<tr>
<td>
2<sup>10</sup>
</td>
<td>1024</td>
<td>10000000000</td>
</tr>

<tr>
<td>
2<sup>11</sup>
</td>
<td>2048</td>
<td>100000000000</td>
</tr>

<tr>
<td>
2<sup>12</sup>
</td>
<td>4096</td>
<td>1000000000000</td>
</tr>
</table>

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<sup>0</sup> as ground state. Every binary integer, that has at least a bit turned on, has this in them. So there is a 2<sup>0</sup> in 2, 4, 8, ..., 2<sup>n</sup>.

## The pattern
  1. Every number except zero has a `1` ready state in its most significant bit, since every number contains a 2<sup>0</sup>.

  2. The number of zeros `0` that follow the `1` from the most significant bit, is the power of 2. So 2<sup>1</sup> = 10<sub>2</sub> = 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 2<sup>n</sup> number is `n + 1`, because the possible outcome of `n` number of bits ranges from 0 to 2<sup>n</sup>-1, not 1 to 2<sup>n</sup>, 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`, 2<sup>3</sup> = 1000<sub>2</sub>.

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

  4. The closest number to `5` from the table is `4`, 100<sub>2</sub>.

  5. Since the number is `5`, we could simply say the remainder is `1` and add it to 100<sub>2</sub>, which gives 101<sub>2</sub>.
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 2<sup>n</sup>-1. Here is another table for that.

<table>
<tr>
<th>Factor - 1</th>
<th>Decimal</th>
<th>Binary</th>
</tr>

<tr>
<td>
2<sup>1</sup> - 1
</td>
<td>1</td>
<td>1</td>
</tr>

<tr>
<td>
2<sup>2</sup> - 1
</td>
<td>3</td>
<td>11</td>
</tr>

<tr>
<td>
2<sup>3</sup> - 1
</td>
<td>7</td>
<td>111</td>
</tr>

<tr>
<td>
2<sup>4</sup> - 1
</td>
<td>15</td>
<td>1111</td>
</tr>

<tr>
<td>
2<sup>5</sup> - 1
</td>
<td>31</td>
<td>11111</td>
</tr>

<tr>
<td>
2<sup>6</sup> - 1
</td>
<td>63</td>
<td>111111</td>
</tr>

<tr>
<td>
2<sup>7</sup> - 1
</td>
<td>127</td>
<td>1111111</td>
</tr>

<tr>
<td>
2<sup>8</sup> - 1
</td>
<td>255</td>
<td>11111111</td>
</tr>

<tr>
<td>
2<sup>9</sup> - 1
</td>
<td>511</td>
<td>111111111</td>
</tr>

<tr>
<td>
2<sup>10</sup> - 1
</td>
<td>1023</td>
<td>1111111111</td>
</tr>

<tr>
<td>
2<sup>11</sup> - 1
</td>
<td>2047</td>
<td>11111111111</td>
</tr>

<tr>
<td>
2<sup>12</sup> - 1
</td>
<td>4095</td>
<td>111111111111</td>
</tr>
</table>

At least you should know this from 1 - 255.
___
I hope you enjoyed this 😍. You can fav it here and follow me on [twitter](https://twitter.com/realongman)


[bit-m]: https://en.m.wikipedia.org/wiki/Bit_manipulation