# Binary & Hexadecimal Math

## From Binary to Decimal

Counting in binary means adding up the powers of 2 where the bits are **on** (meaning they are 1).\
Each bit position represents a power of 2, starting from the right:

```
1   0   0   1
8   4   2   1
```

Here, only 8 and 1 are on

```
8 + 1 = 9
```



***

## From Decimal to Binary

Let’s take the number **176** and convert it to binary. We use these positions (powers of 2):

```
128  64  32  16  8  4  2  1
```

\
Start from the left:

* Is 176 bigger than 128? **Yes!** \
  Turn on the **128 bit** (write 1)

```
176 - 128 = 48

1 0 0 0 0 0 0 0
```



* Is 48 bigger than 64?  **No!** \
  Bit stays 0
* Is 48 bigger than 32? **Yes!** \
  Turn on the **32 bit**

```
48 - 32 = 16

1 0 1 0 0 0 0 0
```



* Is 16 equal to 16? **Yes!**\
  &#x20;Turn on the **16 bit**

```
 16 - 16 = 0
 
 1 0 1 1 0 0 0 0
```



***

## From Decimal to Hexadecimal

Let’s say you want to convert **100** into hexadecimal.

* Is 100 bigger than 256? → No → First hex digit is 0 → `0x0`
* Now ask: how many 16s fit into 100?

We go like this:

```
16, 32, 48, 64, 80, 96 → that’s 6 times
```

So we write: `0x6`. Now subtract 96 from 100 and we get **4,** So the final hex value is: `0x64`
