# Binary Numbers¶

We’re used to counting in a base-10 numbering system, called the decimal system. In the decimal system each place is 10 times larger than the next place and there are 10 number symbols (0 through 9).

Consider the number: 231.

\begin{align} 10^2 \end{align} |
\begin{align} 10^1 \end{align} |
\begin{align} 10^0 \end{align} |
---|---|---|

100’s place |
10’s place |
1’s place |

2 |
3 |
1 |

Binary is a base-2 counting system, where there are just two number symbols, 0 and 1. Binary is the simplest possible counting system. It works for today’s computers because 0 represents “off” and 1 represnts “on”, the two states of a CMOS gate.

Here’s the number 231 in binary.

\begin{align} 2^7 \end{align} |
\begin{align} 2^6 \end{align} |
\begin{align} 2^5 \end{align} |
\begin{align} 2^4 \end{align} |
\begin{align} 2^3 \end{align} |
\begin{align} 2^2 \end{align} |
\begin{align} 2^1 \end{align} |
\begin{align} 2^0 \end{align} |
---|---|---|---|---|---|---|---|

128’s place |
64’s place |
32’s place |
16’s place |
8’s place |
4’s place |
2’s place |
1’s place |

1 |
1 |
1 |
0 |
0 |
1 |
1 |
1 |

As you can see it takes more digits to represent a number in binary than it does in decimal. The word “bit” is short for “binary digit”.

## Converting from Decimal to Binary¶

Converting from decimal to binary is easy once you know the trick. You do it with a two nstep *algorithm*. Follow these steps.

Is your number even or odd? If it’s odd write a 1. If it’s even write a 0.

Divide your number by 2, ignore any fractional part.

Repeat until you get to zero!

Here’s how to convert 231.

Step |
Number |
Even or Odd? |
Bits so far |
---|---|---|---|

1 |
231 |
Odd |
1 |

2 |
115 |
Odd |
11 |

3 |
57 |
Odd |
111 |

4 |
28 |
Even |
0111 |

5 |
14 |
Even |
00111 |

6 |
7 |
Odd |
100111 |

7 |
3 |
Odd |
1100111 |

8 |
1 |
Odd |
11100111 |

9 |
0 |
Stop! |
11100111 |

## Converting from Binary to Decimal¶

Converting from binary to decimal is also easy. Write your binary number and add together all of the place values where your number has a one. Ignore the zeros.

128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
---|---|---|---|---|---|---|---|

1 |
1 |
1 |
0 |
0 |
1 |
1 |
1 |

128 |
64 |
32 |
4 |
2 |
1 |

128 + 64 + 32 + 4 + 2 + 1 = 231

## Exercise¶

Pick a few numbers and convert them. Use my program to check your work.

```
[ ]:
```

```
number = int(input("Please enter an integer: "))
binary = ''
while number > 0:
if number % 2 == 0:
binary = '0' + binary
print (f'{number} is even. ({binary})')
else:
binary = '1' + binary
print (f'{number} is odd. ({binary})')
number = int(number / 2)
print ("All done!")
print ("The binary is:", binary)
```

# Hexadecimal¶

Binary is hard to work with because it takes a lot of bits to write most numbers. Decimal is hard to work with because it’s cumbersome to convert between binary and decimal. So what’s a nerd to do?

The hexadecimal counting system is a base-16 counting system. It has 16 number symbols. Hexadecimal borrows the letters A through F to represent number values.

Symbol |
Value |
---|---|

0-9 |
Same as decimal |

a |
10 |

b |
11 |

c |
12 |

d |
13 |

e |
14 |

f |
15 |

The great thing about hexadecimal is that you can convert four bits to a decimal digit easily. Here’s how.

Bits |
Hex |
---|---|

0000 |
0 |

0001 |
1 |

0010 |
2 |

0011 |
3 |

0100 |
4 |

0101 |
5 |

0110 |
6 |

0111 |
7 |

1000 |
8 |

1001 |
9 |

1010 |
a |

1011 |
b |

1100 |
c |

1101 |
d |

1110 |
e |

1111 |
f |

Hexadecimal numbers are often written with a “0x” at the beginning. That makes it harder to confuse them with decimal numbers. So when you see “10” think *ten* and when you see “0x10” think *sixteen*. When you convert binary to hexadecimal split your binary number into groups of four bits.

Here’s how to convert 231:

1110 |
0111 |
---|---|

e |
7 |

So…

230 = 11100111 (binary) = 0xe7