# Binary & Hexadecimal

# Binary Numbers

A modern electronic computer consists of millions of transistors which work like switches. Each switch can be either OFF or ON. We can use the two possible states of a switch to represent the numbers 0 and 1. By combining many switches together we can represent larger numbers using patterns of zeros and ones – such numbers are called **binary numbers**, or base 2 numbers. Since humans like to count using their ten fingers and thumbs they prefer to use **decimal numbers**, or base 10 numbers. You should know how to convert between binary numbers and decimal numbers.

Consider the decimal number **6247**. We read this as ‘six thousand two hundred forty seven’ and would understand that it is made up six thousands, two hundreds, four tens and seven units. The position of each digit tells us whether that digit represents thousands, hundreds, tens or units. In your math class you should have learnt that 1000= 10³, 100= 10², ten = 10 to the power of 1 and 1 = 10 to the power of 0. Note that any number to the power of zero is equal to one. So, to summarise, the meaning of each digit is indicated in the table below.

Binary numbers are interpreted in a similar way, but using base two instead of ten. Consider the binary number 110101.

We see that our number is made up of a thirty-two, a sixteen, a four and a one, so this binary number is equivalent to the decimal number **53** (32+16+4+1=53).

**Converting Binary numbers to decimal numbers**

As explained above, it is easy to convert a binary number to a decimal number by considering the meaning of each digit (it’s best to start with the right-most digit). For practice, try converting the binary number 10111 to decimal, then check your answer at the bottom of this page.

Of course, just because a number is made up of ones and zeros does not automatically mean it is a binary number. In the above text you knew that 10111 was a binary number because the text explicitly said so, but the base of the number can also be indicated with a **subscript **(unfortunately new Google Sites does not allow me to demonstrate this in this page). Whereas the number 10111 (base 10) can be read ‘ten thousand one hundred eleven’ the binary number 10111 should not be read like that for these binary digits or ‘bits’ do NOT represent thousands etc. The binary number 10111 should be read ‘one zero one one one’.

**Converting decimal numbers to binary numbers**

How would you convert the decimal number **139** into binary form? The quickest method is this:

1. Divide the number by two using ‘integer division’ to give an answer in the form of an integer plus a remainder.

2. Keep dividing the integer results by two until there is nothing left to divide (it’s best to do this in a vertical format as shown below)

3. Now read the remainders * from bottom to top* or allow the column of remainders to ‘fall to the right'.

Example:

Reading the remainders * from bottom to top*, we can say

**139 (decimal)= 10001011 (binary)**.

Scientific calculators often have the possibility of converting between binary and decimal. If you have a scientific calculator, see if you can use your calculator to verify the above equivalence. The method may differ from one calculator to another. On my calculator I would enter the number 139 in normal decimal mode and then switch to binary mode by pressing the ‘MODE’ button then the ‘BIN’ button’.

For practice, try converting the decimal number 95 into binary, then check your answer at the bottom of this page.

You can’t help noticing that even a fairly small number like 95 looks horribly long when written in binary. Working with such a long string of ones and zeros it is very easy to make mistakes. To avoid working with binary numbers we can always convert them to decimal, but that can be rather tedious if you don’t have a scientific calculator handy. To avoid having to work in binary or having to convert to between binary and decimal, computer programmers often like to use **hexadecimal numbers**, which you will read about next.

# Hexadecimal numbers

We note that ‘**hex’** means ‘**six’** and ‘**deci’** means ‘**ten’** so ‘**hexadecimal’** means **sixteen **(6+10). Computer programmers like to work in base 16 because it is very easy to convert binary numbers to hexadecimal and back again. Unlike base 2 numbers which contain only the digits 0 and 1, and decimal numbers which contain the digits 0-9, hexadecimal numbers consist of the digits 0-9 and the letters A-F. The meaning of these letters is given in the table below:

To easily convert binary numbers to hexadecimal (sometimes called simply ‘hex’) split the binary number into groups of four binary digits (4 bits), starting at the right end. Then use the table above to convert each group into a single hexadecimal character. For example, consider the binary number **1101101**. Splitting this into groups of 4 bits each we get 110/1101. Using the table above to convert each of these binary numbers into hex we get **6D**. We can write **11011012 (binary) = 6D16 (hex) . **Note that the rightmost digit in this hex number represents units and the next digit represents sixteens, so we have 6 sixteens and thirteen (D) units. So 6D16 = (decimal) 6*16 + 13*1 = 109. (You probably already know that computer programmers use an asterisk to represent multiplication.)

To convert a hex number into binary just use the table above. For example **B3 (hex) = 10110011 (binary)**. Note how the ‘3’ in the hex number was expressed as the 4 bit number 0011 rather than the two bit number 11 shown in the table.

As already noted, most scientific calculators can easily convert between decimal and binary – such calculators can also convert to and from hex notation.

There’s more to binary and hex than just conversions – regular operations such as addition, subtraction and multiplication can be done in any of these bases. For example, in binary, 1+1=10, and in hex C-A=2. Such calculations can also be done on your scientific calculator.

You may have noticed that your calculator can also work in base 8, called **octal**, but that is less used than the other bases mentioned here and is just mentioned in passing.

Now that you know a bit about binary numbers you are ready to learn about **LOGIC GATES**, some of the most important components in digital electronic circuits. Click **HERE** to continue.

## Answers to questions

The binary number 10111 is equivalent to the decimal number 23 for the binary number contains (reading from the right), a one, a two, a four, NO eights, and a sixteen. 16+4+2+1=23. So we can say 10111 (binary) = 23 (decimal)

95 (decimal) = 1011111 (binary)