4 NumberRepresentation
4 NumberRepresentation
Numeral Systems
A Brief History of Numbers
●
Archaeologists don’t know when
exactly counting or representing
numbers really began
●
First likely evidence was found in
Central Africa on a bone from
around 20,000 years ago
●
Contains notches which may be the
construction of a numeral system
= one goat
= two goats
= three goats
= ??? goats
A Brief History of Numbers
●
We can use the digit ‘1’ to replace the stones:
1 = one goat
11 = two goats
111 = three goats
1111111111111111111111111111111 = ??? goats? (31)
●
This number system is called a unary numeral system
●
Although it is unwieldy, it is still used in some theoretical
computer science problems.
●
Any practical application you know of ?
A Brief History of Numbers
I 1 160 = C + LX = CLX
V 5 207 = CC + VII = CCVII
X 10 1009 = M + IX = MIX
L 50
C 100 1066 = ?M + LX + VI = MLXVI
D 500
M 1000 MCMXCIX = ?1999
A Brief History of Numbers
A Brief History of Numbers
●
Our current numeral system is the Hindu-Arabic system
which is also known as the decimal numeral system
●
It contains ten symbols:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
●
Decimal is from the Greek déka (δέκα) which means ten
A Brief History of Numbers
A Brief History of Numbers
A Brief History of Numbers
Representing Decimal Numbers
●
As stated earlier, the ten symbols (in increasing value) are
0, 1, 2, 3, 4, 5, 6, 7, 8, and 9
●
Suppose we have the number 9 and wish to add 1 to it
– 9 is the largest value of the ten symbols
– There is no other single symbol that can represent 9 + 1
– What do we do?
Representing Decimal Numbers
●
We put an additional 1 symbol to the left of the 9, and
wrap the symbol in the position where the 9 was, back to
the lowest valued symbol, 0
9
+1
10 This value, we call ten
Representing Decimal Numbers
●
In the decimal system, we have “places.”
– The rightmost place is the ones place
– The next rightmost is the tens place
– The next is the hundreds place
– And so on…
●
The value of the number is thought of as the
sum of the values in each of the places
Representing Decimal Numbers
●
Example: Take the number 43,972
●
This can be thought of as:
– 2 ones
– 7 tens
– 9 hundreds
– 3 thousands
– 4 ten-thousands
●
All added together, they create the value
forty three thousand nine hundred seventy two
Representing Decimal Numbers
●
To go further, we can think of each place in a decimal number as a
power of ten
0
10 = 1
1
10 = 10
2
10 = 100
3
10 = 1000
4
10 = 10,000
●
And so on...
Representing Decimal Numbers
●
So, we can think of the number 43,972 in terms of powers of ten:
0
2 x 10 = 2 x 1 = 2
1
7 x 10 = 7 x 10 = 70
2
9 x 10 = 9 x 100 = 900
3
3 x 10 = 3 x 1000 = 3000
4
4 x 10 = 4 x 10,000 = 40,000
●
Adding these numbers together, we get 43,971
Representing Decimal Numbers
●
This method works with ANY number in the decimal system – even
with fractional numbers:
-2
2 x 10 = 2 x 0.01 = 0.02
-1
7 x 10 = 7 x 0.1 = 0.7
0
9 x 10 = 9x1=9
1
3 x 10 = 3 x 10 = 30
●
Adding these values gives us 39.72
Other Number Systems
●
Not only does this system work for the decimal
number system, it works on other number
systems as well
Binary Numbers
Binary Numbers
●
The term binary probably comes from Latin
– bini (adjective) – pair, or two together
●
As the name implies, there are two different
symbols: 0 and 1
●
In Computer Science, we call a single binary
digit a bit
Gottfried Leibniz
●
German philosopher and mathemetician
●
Developed Calculus independently from Isaac
Newton
●
1703 wrote Explanation of Binary Arithmetic, which
uses only the characters 1 and 0, with some remarks
on its usefulness, and on the light it throws on the
ancient Chinese figures of Fu Xi (required reading)
Gottfried Leibniz
●
Leibniz’s system of counting does not follow the
pattern of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and then starting
over with 10, 11, 12, …
●
Instead, he counts 0, 1, then starts over with 10, 11,
and continues with 100, 101, 110, 111, 1000 and so on
●
Claims that it is the most fundamental method that can
be used for counting
●
“… as numbers are reduced to the simplest principles,
like 0 and 1, a wonderful order is apparent throughout.”
Representing Binary Numbers
●
We follow the example for decimal numbers
– Binary numbers also have places associated with its
numbers
– Instead of ones, tens, hundreds, and so on, the places in
binary (from right to left, or lowest to highest) are one, two,
four, eight, sixteen, and so on
●
The places in binary are not plural (unlike for decimal)
– There can only be a single value (1) or no value (0) in its
place
Representing Binary Numbers
●
Example: Given a binary number 1001011
– We break it down (from right to left) as follows:
1 one
1 two
0 four
1 eight
0 sixteen
0 thirty-two
1 sixty-four
1 + 2 + 8 + 64 = 75
●
So 1001011 in binary is the value 75 in decimal
Representing Binary Numbers
●
Instead of using powers of 10 for the decimal system, the binary system uses
powers of 2
0
2 =1
1
2 =2
2
2 =4
3
2 =8
4
2 = 16
5
2 = 32
6
2 = 64
and so on.
Representing Binary Numbers
●
Like the decimal system, we think of the number 1001011 in the binary system
as:
0
2 = 1x1 = 1
1
2 = 2x1 = 2
2
2 = 4x0 = 0
3
2 = 8x1 = 8
4
2 = 16 x 0 = 0
5
2 = 32 x 0 = 0
6
2 = 64 x 1 = 64
●
Adding these values together gives us
1 + 2 + 0 + 8 + 0 + 0 + 64 = 75
Practice
●
What is the decimal system equivalent of the
binary number 10101010 ?
Write down the full analysis
Number Bases
●
The number of symbols used in a number system is also
referred to as the “Base” of the number system
●
Decimal is also called Base 10 and uses powers of 10
●
Binary is also called Base 2 and uses powers of 2
●
Sometimes subscripts are used to avoid confusion regarding
the base of the number
– What is the value of 101?
– 10110 is the value one hundred one
– 1012 is the value five
Converting Between Bases
●
Sometimes it is useful to convert values
between bases
●
Converting from Base 2 to Base 10 has been
shown in the preceeding slides
●
Converting from Base 10 to Base 2 is not as
straightforward
Converting from Base 10 to Base 2
●
Method 1 – The reverse of the method to go
from Base 2 to Base 10
●
It is useful to memorize the powers of two up
15
through about 2 . Of course you can always
derive them as needed.
●
The next slide has a table of powers of 2 up
15
through 2
Powers of 2
0 4 8 12
2 =1 2 = 16 2 = 256 2 = 4096
1 5 9 13
2 =2 2 = 32 2 = 512 2 = 8192
2 6 10 14
2 =4 2 = 64 2 = 1024 2 = 16384
3 7 11 15
2 =8 2 = 128 2 = 2048 2 = 32768
Convert 2163 to Binary – Method 1
●
Given a number: 2163
– Find the highest power of 2 that is less than or equal to that
number
11
●
In this case, it is 2048 (or 2 )
●
Place a 1 in the binary position for that value:
1
– Subtract 2048 from the original value:
2163 – 2048 = 115
Convert 2163 to Binary – Method 1
●
Now we have 115 as the remainder
– Find the highest power of 2 that is less than or equal to 115
6
●
In this case, it is 64 (or 2 )
●
Place a 1 in the binary position for that value:
1 1
– Subtract 64 from 115:
115 – 64 = 51
Convert 2163 to Binary – Method 1
●
Now we have 51 as the remainder
– Find the highest power of 2 that is less than or equal to 51
5
●
In this case, it is 32 (or 2 )
●
Place a 1 in the binary position for that value:
1 11
– Subtract 32 from 51:
51 – 32 = 19
Convert 2163 to Binary – Method 1
●
Now we have 19 as the remainder
– Find the highest power of 2 that is less than or equal to 19
4
●
In this case, it is 16 (or 2 )
●
Place a 1 in the binary position for that value:
1 111
– Subtract 16 from 19:
19 – 16 = 3
Convert 2163 to Binary – Method 1
●
Now we have 3 as the remainder
– Find the highest power of 2 that is less than or equal to 3
1
●
In this case, it is 2 (or 2 )
●
Place a 1 in the binary position for that value:
1 111 1
– Subtract 2 from 3:
3–2=1
Convert 2163 to Binary – Method 1
●
Now we have 1 as the remainder
– Find the highest power of 1 that is less than or equal to 1
0
●
In this case, it is 1 (or 2 )
●
Place a 1 in the binary position for that value:
1 111 11
– Subtract 1 from 1:
1–1=0
Convert 2163 to Binary – Method 1
●
Now that we have 0 as the remainder, we can
stop
– Fill in the rest of the empty spaces with 0s
100001110011
– So, the binary or base 2 representation of the value
216310 is 1000011100112
Practice
●
What is the binary system equivalent of the
decimal number 7021 ?
Write down the full analysis
A note about Zeros
●
You can always put 0s to the left of a number without changing its value
– Examples:
●
The decimal value 3754 is equal to 00003754
●
11001101 in binary is equal to 000000011001101
●
However in some computers languages, a leading 0 for a number
means the number is represented in a specific base – Octal. We will
discuss octal shortly
●
Binary sometimes uses multiples of four bits with leading 0s as
necessary: 0011001110112
● Sometimes the bits are also grouped by fours: 0011 0011 10112
Convert Decimal to Binary – Method 2
●
Method 2 works by placing values from right to left, or
lowest place to highest (opposite of what Method 1 does)
●
It works by using the remainder of an integer division by
2 (which will be either 0 or 1)
●
For example, if we divide the value 7 by 2, we would get
3, with a remainder of 1
●
If we divide the value 8 by 2, we would get 4 with a
remainder of 0
Convert 2163 to Binary – Method 2
●
Start with our original number, 2163
●
Dividing 2163 by 2 results in 1081 with a
remainder of 1. So we put a 1 in the lowest
(one) place:
1
●
Now we continue with the value 1081
Convert 2163 to Binary – Method 2
●
Dividing 1081 by 2 results in 540 with a remainder of 1.
So we put a 1 in the next lowest (two) place:
11
●
Continuing with the value 540, dividing it by 2 results in
270 with a remainder of 0. So a 0 goes in the next
lowest (four) place:
011
●
Now we continue with the value 270
Convert 2163 to Binary – Method 2
●
Dividing 270 by 2 results in 135 with a remainder of 0.
So place a 0 in the next lowest place:
0011
●
Continuing with the value 135, dividing it by 2 results in
67 with a remainder of 1. So a 1 goes in the next
lowest place:
10011
●
Now we continue with the value 67
Convert 2163 to Binary – Method 2
●
Dividing 67 by 2 results in 33 with a remainder of 1. So
place a 1 in the next lowest place:
110011
●
Continuing with the value 33, dividing it by 2 results in
16 with a remainder of 1. So a 1 goes in the next
lowest place:
1110011
●
Now we continue with the value 16
Convert 2163 to Binary – Method 2
●
Dividing 16 by 2 results in 8 with a remainder of 0. So
place a 0 in the next lowest place:
01110011
●
Continuing with the value 8, dividing it by 2 results in 4
with a remainder of 0. So a 0 goes in the next lowest
place:
001110011
●
Now we continue with the value 4
Convert 2163 to Binary – Method 2
●
Dividing 4 by 2 results in 2 with a remainder of 0. So
place a 0 in the next lowest place:
0001110011
●
Continuing with the value 2, dividing it by 2 results in 1
with a remainder of 0. So a 0 goes in the next lowest
place:
00001110011
●
Now we finish up with the value 1
Convert 2163 to Binary – Method 2
●
Dividing 1 by 2 results in 0 with a remainder of 1. So place a 1
in the last place:
100001110011
●
Once the final division by 2 results in the value 0, we are done
●
So, the binary or base 2 representation of the value 2163 10 is
1000011100112
●
Continuous divisions of 0 by 2 will result in 0 with a remainder
of 0. This would merely add 0s to the left of the number, which
would not change its value.
Practice
●
What is the binary system equivalent of the
decimal number 8191 ?
Use method2 (repetitive division)
Other Bases in Computer Science
●
At the lowest level, computers work with only 0s
and 1s
●
Working directly with binary values is still done in
some CS disciplines (i.e. micro-coding)
●
When binary values get large, it becomes difficult to
see a difference between one value and another.
Other Bases in Computer Science
Can you spot the difference between these two
numbers?
11100100100111101100110011101001
1110010010011101100110011101001
Other Bases in Computer Science
●
It is easier to spot the difference if they were lined up:
11100100100111101100110011101001
1110010010011101100110011101001
●
But most of the time, that isn’t the case
●
It is easier to see the difference if the values are
represented in a different base:
3,835,612,39310 and 1,917,766,88910
Other Bases in Computer Science
●
Besides binary and decimal, two other bases
commonly used in computer science are:
– Hexadecimal (Base 16)
– Octal (Base 8)
●
Other bases are used, such as Base 4 and
Base 32, but much less often
Hexadecimal (Base 16)
●
Just like with the Decimal and Binary number
systems, which have 10 and 2 symbols
respectively, the Hexadecimal number system
has 16 symbols
●
Hexa (from Greek) means six, and Decimal
means ten. So, six plus ten makes sixteen
Hexadecimal
●
The symbols used for Hexadecimal are:
0 – zero 4 – four 8 – eight C – twelve
1 – one 5 – five 9 – nine D – thirteen
2 – two 6 – six A – ten E – fourteen
3 – three 7 – seven B – eleven F – fifteen
●
It usually doesn’t matter if the alpha characters
are upper-case or lower-case
Hexadecimal
●
Hexadecimal works in the same manner that
decimal and binary work, except instead of
powers of 10 and 2 respectively for the places in
the number, it uses powers of 16 for the places in
the number.
●
Hexadecimal numbers often have a leading 0x
symbol to specifically show that the number is
hexadecimal (e.g. 0x4739)
Hexadecimal
● Given a value in hexadecimal, such as 873 16 (or 0x873)
– Use the same method used for binary and decimal values
0
3 x 16 = 3 x 1 = 310
1
7 x 16 = 7 x 1610 = 11210
2
8 x 16 = 8 x 25610 = 204810
0 – zero 4 – four
1 – one 5 – five
2 – two 6 – six
3 – three 7 – seven
Octal
●
Octal works just like decimal, binary and
hexadecimal work, except it uses powers of 8 for the
places in the number.
●
Although in some languages (C for example), an
octal value is denoted with a leading 0 (like
hexadecimal is denoted with a leading 0x), generally
octal values use 8 as a subscript (e.g. 4318) or is
explicitly stated to be an octal representation.
Octal
● Given a value in Octal, such as 473 8
– Use the same method used for other number systems
0
3 x 8 = 3 x 1 = 310
1
7 x 8 = 7 x 810 = 5610
2
4 x 8 = 4 x 6410 = 25610