# How to Convert Between base-10, Hexadecimal, and Binary

## Step 2: Base Basics (Con't): Binary

Binary (base-2)

As the name implies, a binary numbering system consists of two numbers: 1 and 0. Accordingly, this is referred to as base-2 and numbers look like 100100102. In such a system, the representation of values are accomplished through a "power of two" system. Each place in a binary number, often referred to as a bit, has a particular value that can easily be determined. Binary numbers are usually written as a multiple of eight, or a byte. Sometimes, only four bits of the byte, either the upper or lower half are used and these four bits are collectively called a nibble. Nibbles become important when converting between bases as we will see later.

Without digressing, I will be using big endian notation where integers are stored in memory starting with the most significant bit (MSB) and ending with the least significant bit (LSB). The value of any given bit location is determined by taking two (the number of digits in our binary system) to the power of the bit's place using a zero-based index: 2bit. For instance, the LSB of the nibble is 20 or 1. The fourth bit, that is, the third place (remember we start at zero) value is 23 or 8. Let's just look at some examples to make it clearer. Here's how a nibble is represented in binary.

```Zero-based index    3   2   1   0
Bit place          4th 3rd 2nd 1st
Bit value           8   4   2   1
Example values:

Decimal 12          1   1   0   0
Decimal 5           0   1   0   1
```

If the bit place's value is to be added (or counted) that bit will be a 1, else it will be a 0. Knowing this, the decimal value "1" is written "0001" because the 1st bit is set and that value is one. The decimal value "10" is written in binary as "1010" and if you add the bit place's value where there is a 1 then you will see it adds to 10. In this way, a nibble can represent the numbers 0 through 15. Larger numbers require the use of the high-order nibble.

```Bit place    8th 7th 6th 5th 4th 3rd 2nd 1st
Bit value    128  64  32  16  8   4   2   1
Example value:
Decimal 88    0    1   0   1  1   0   0   0
Decimal 253   1    1   1   1  1   1   0   1
```

Add the bit values for each bit's place that has a 1 to satisfy yourself that the decimal values are accurately represented by the binary value to its right.

I will cover binary addition, subtraction, multiplication, division, floating points, and negative numbers via 2's complement and 1's complement in a later instructable I've already started working on called How to Calculate like a Microcontroller, but here's a preview as an enticement (yeah, like binary math can be an enticement, right?) to view and rate my other instructables. :)

Preview of How to Calculate Like a Microcontroller.

Binary arithmetic is very simple and straight-forward. You should familiarize yourself with the binary representation of numbers zero through three, if you don't already have these committed to heart. They are 00, 01, 10, and 11. The primary thing to remember is to carry over ones and that's the hardest thing you'll have to do, so breathe easy.

I'm going to add 15810 and 5110 in binary, which if you have already added them should yield 20910. Follow along and double-check my calculations.

```     1001 1110
+ 0011 0011
-------------
```
Now, add down from the LSB (far right) moving to the left. The first is 0 + 1 which is 1. The next is 1 + 1 which is 2, but in binary is 10. This is two digits so we have to carry that one over to the next column.
```     1001 1110
+ 0011 0011
-------------
01
-------------
1
```

In that column, it's 1 + 0 + 1 which is again two, or 10. Carry that one. Continue.
```     1001 1110
+ 0011 0011
-------------
0001
-------------
1111
```
Now we have 1 + 1 + 1 which is three, or, yes, you got it: 11. So instead of putting a zero and carrying a 1, put a 1 and carry a one. Continue to the end.
```     1001 1110
+ 0011 0011
-------------
1101 0001
-------------
111111
```
The 110100012 is 20910, so we did our calculations correctly.

Binary Arithmetic: Multiplication and Division
In the base-2 system, division and multiplication by a power of two is also easily accomplished by right and left shifting, respectively. Shifting is exactly as it sounds: when you left shift by n, you take all the 1's and 0's and move them n places to the left filling in the shifted places with a 0. When you logically right shift by n places, you move all the binary values n places to the right, filling the moved slots with 0's (there is also an arithmetic right shift, but I don't cover it here). In the example below, I shift the number 4 to the left by one bit. The bit #1 is the 2nd from the right in our zero-based index system, and we can also write it 21 or 2. This should cause a multiplication of 4 * 21 or 8.
```Left shift:   4 << 1
Bit value:    8 4 2 1
Binary: 4 =   0 1 0 0
Shifted to the left one bit:
1 0 0 0  = 8 denary
```

You can see how shifting to the left changes the number from four to eight, effectively multiplying the value by two. We could also shift left by two bits or any number of bits from 1 to n - 1, where n is the number of bits in our binary number. Accordingly, shifting a binary number to the right effectively divides the number by a power of two. Think about that if you're not clear then take a look below. Here we shift 32 two bits to the right, effectively dividing 32 by 22 , yielding 8.

```Right shift: 32 >> 2
Bit value:  128 64 32 16 8 4 2 1
Binary 32 =  0   0  1  0 0 0 0 0
Shifted to the right two bits:
0   0  0  0 1 0 0 0
```

That's far from all of the foundational binary operations, but it's an adequate coverage for our lil' 'ol instructable here.
Remove these ads by Signing Up