Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Number Systems

Dr. Liam Naughton

1 Binary Numbers
Normally in mathematics numbers are represented using the decimal number system.
that is, they are written in base 10. For example the decimal number 1325 is

1325 = 5 ×100 + 2 ×101 + 3 ×102 + 1 ×103 .


This is the simple “units, tens, hundreds, thousand” terminology that we learn at
a very young age. The reason that we use the decimal system to represent numbers is
probably related to the fact that humans have ten fingers and ten toes (that’s not a joke).
But there is no reason why we can’t use a different system or base to represent numbers.

Definition 1.1. An integer a written in base b expansion is denoted by (a)b .

Example

What decimal number is represented by the base 3 number (231)3 ?

(231)3 = 1 ×30 + 3 ×31 + 2 ×32


= 1 + 9 + 18
= 28

You will have seen that computers use binary numbers (base 2) rather than say base
10 or any other base. This is because computers are built upon a digital electronic
foundation where “switches” or electrical circuits can be either ON or OFF.
Converting a decimal number to a given base requires repeatedly dividing the number
by the base and then recording the remainders.

1
Example
Convert 231 to binary.

231 = 2.115 + 1
.
115 = 2.57 + 1
.
57 = 2.28 + 1
.
28 = 2.14 + 0
.
14 = 2.7 + 0
.
7 = 2.3 + 1
.
3 = 2.1 + 1
.
1 = 2.0 + 1

Now we read the remainders (in black) from bottom to top to obtain the binary
representation. So (231)10 = (11100111)2 .

Exercises
Convert each of the following decimal numbers to binary numbers.

(i) 11. (answer: 1011)

(ii) 123. (answer: 1111011)

(iii) 201 (answer: 11001001)

(iv) 529. (answer: 1000010001)

Example

Convert (10101101)2 to decimal.

(10101101)2 = 1 ×20 + 0 ×21 + 1 ×22 + 1 ×23 + 0 ×24 + 1 ×25 + 0 ×26 + 1 ×27


= 1 + 4 + 8 + 32 + 128
= 173

2
Exercises
Convert each of the following binary numbers to decimal numbers.

(i) 1111. (answer: 15)

(ii) 1010110. (answer: 86)

(iii) 1110001 (answer: 113)

(iv) 110110001. (answer: 433)

Arithmetic operations are possible on binary numbers just as they are on decimal num-
bers. In fact the procedures are quite similar in both systems.
1.1 Binary Addition
When adding binary numbers we are really only bound by three simple rules:

• 0 + 0 = 0.

• 0 + 1 = 1.

• 1 + 1 = 2 = (10)2 .

Notice that adding two single digit “1’s” produces a two digit result, which means that
we have to “carry” into the next place, just as with decimal addition. Let’s take an easy
example. We know that 2 + 4 = 6 in base 10. Let’s illustrate this in binary.

Example

Recall that (2)10 = (10)2 and (4)10 = (100)2 and that (6)10 = (110)2 .

0 1 0
+ 1 0 0
1 1 0

Now let’s look at a slightly more complicated example which requires us to “carry”.
Example

Recall that (7)10 = (111)2 and (5)10 = (101)2 and that (12)10 = (1100)2 .

1 1 1
+ 1 11 01 1
1 1 0 0
Here, we use the small red digits to represent the digit being “carried”.

Now try the following exercises.

3
Exercises
Carry out each of the binary additions below.

(i) 111 + 111. (answer: 1110)

(ii) 101101 + 100001. (answer: 1001110)

(iii) 11101 + 1000 (answer: 100101)

Use an online tool to carry out the additions using decimal numbers and verify that
your answers (and mine) are correct.

1.2 Binary Subtraction


There are a few equivalent methods to carry out binary subtraction. The method we will
use can be summarised by the phrase “flip the bits and add 1 and add ”. We illustrate the
technique using the example (10)10 − (5)10 = (5)10 . We know that (10)10 = (1010)2 and
that 510 = (101)2 .
Example
We start by appending leading zeros so that both numbers have the same number
of digits.
1 0 1 0
− 0 1 0 1

Now we “flip the bits” of the number below. That is to say we replace every 1 by
a 0 and every 0 by a 1.
f lipping
0101 ====⇒ 1010
Now we add 1 to this flipped number.
1 0 1 0
+ 1
1 0 1 1
And now we add this number to 1010.
1 0 1 0
+ 1 0 1 1
1 0 1 0 1
Finally, we discard the leftmost digit to obtain our answer.
1 0 1 0
− 1 0 1
1 0 1

This technique is a little confusing at first so just to be sure we’ll do one more example.

4
Example

Let’s compute (15)10 − (10)10 = (5)10 . We know that (15)10 = (1111)2 and that
(10)10 = (1010)2 and that (5)10 = (101)2 . We want to compute

1 1 1 1
− 1 0 1 0

We start by “flipping the bits” of the number below. That is to say we replace every
1 by a 0 and every 0 by a 1.
f lipping
1010 ====⇒ 0101

Now we add 1 to this flipped number.


0 1 0 1
+ 1
0 1 1 0
And now we add this number to 1111.
1 1 1 1
+ 0 1 1 0
1 0 1 0 1
Finally, we discard the leftmost digit to obtain our answer.
1 1 1 1
− 1 0 1 0
0 1 0 1

Now try the following exercises.


Exercises
Carry out each of the binary additions below.

(i) 111 − 111. (answer: 000)

(ii) 101101 − 100001. (answer: 01100)

(iii) 11101 − 1000 (answer: 010101)

Use an online tool to carry out the additions using decimal numbers and verify that
your answers (and mine) are correct.

5
1.3 Binary Multiplication & Division
Binary multiplication & division are carried out in exactly the same fashion as deci-
mal multiplication. We will provide a single example of each technique for illustrative
purposes.
Example

Below we illustrate (4)10 × (12)10 = (48)10 .

0100 [ 4 in decimal]
× 1100 [ 12 in decimal]
+ 0000 [ 0100 × 0]
+ 0000 [ 0100 × 0, shifted left]
+ 0100 [ 0100 × 1, shifted left two positions]
+ 0100 [ 0100 × 1, shifted left three positions]
0110000 [ 48 decimal]

Example
Let’s compute 95 ÷ 5 = 19 in binary.
 10011
101 1011111
101
111
101
101
101
0

The point to remember here is that numbers written in different bases are still
numbers. This means that no matter what base we are using we can carry out the
arithmetic operations of addition, subtraction, multiplication and division. There will be
different technicalities involved in the methods depending on the base we use but that
does not present any difficulty for a computer.
1.4 Octal and Hexadecimal Numbers
We can represent numbers in other bases too!
Example

Find the base 10 expansion of (364)7 .

(364)7 = 4.70 + 6.71 + 3.72 = 4 = 42 + 147 = 193.

One base which is used quite often in computer science is base 8 or octal. Base 8 is
especially useful since it relates to binary in a particularly nice way. This is because 8 is

6
a power of two. This means that octal numbers can be made from binary numbers by
grouping consecutive binary digits into groups of three starting from the right as shown
below.
Example

(101010111010)2 = (2746)10
101 010 111 010
5 2 7 2 (5272)8

Each group of three binary digits evaluates to a number from {0, 1, 2, 3, 4, 5, 6, 7}.
We can check that

(5272)8 = 2 × 80 + 7 × 81 + 2 × 82 + 5 × 83 = (2746)10 .

Another particularly useful base is the next power or two, base 16, called hexadec-
imal. there are sixteen hexadecimal digits. They are the same as the decimal digits up
to 9 and then they include the letters A, B, C, D, E, F in place of the decimal numbers
10, 11, 12, 13, 14, 15. For example
Example

(7A2EC)16 = 12 × 160 + 14 × 161 + 2 × 162 + 10 × 163 + 7 × 164 = (500460)10 .

Hexadecimal numbers can be made from binary numbers by grouping consecutive binary
digits into groups of four starting from the right as shown below.
Example

(101010111010)2 = (2746)10
1010 1011 1010
10 = A 11 = B 10 = A (ABA)16

Each group of four binary digits evaluates to a number from


{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}. We can check that

(ABA)16 = 10 × 160 + 11 × 161 + 10 × 162 = (2746)10 .

1.5 Summary
We use decimal numbers in most real life scenarios but we can represent numbers in any
base we like if we so desire. Represented in another base a number is still a number
and can be added, subtracted and so on and so forth. Computers are built upon a
foundation of “switches” or “circuits” which are either “on” or “off”. Since these “switches”
have only two values they can be represented most easily using binary numbers. Binary
numbers themselves can become quite long and they can easily be converted to octal or
hexadecimal for easier manipulation.

You might also like