Chapter 2
Binary Values and Number
Systems
Numbers
2 2
Natural numbers, a.k.a. positive integers
Zero and any number obtained by repeatedly adding one to it.
Examples: 100, 0, 45645, 32
Negative numbers
A value less than 0, with a – sign Examples: -24, -1, -45645, -32
3 3
Integers
A natural number, a negative number, zero Examples: 249, 0, - 45645, - 32
Rational numbers
An integer or the quotient of two integers Examples: -249, -1, 0, 3/7, -2/5
Real numbers
In general cannot be represented as the quotient of any two integers. They have an infinite # of fractional digits.
Example: Pi = 3.14159265…
2.2 Positional notation
4 4
How many ones (units) are there in 642?
600 + 40 + 2 ? Or is it
384 + 32 + 2 ? Or maybe…
1536 + 64 + 2 ?
Positional Notation
5 5
642 is 600 + 40 + 2 in BASE 10
The base of a number determines how many digits are used and the value of each digit’s position.
To be specific:
• In base R, there are R digits, from 0 to R-1
• The positions have for values the powers of R, from right to left: R0, R1, R2, …
Positional Notation
6 6
In our example:
642 in base 10 positional notation is:
6 x 102 = 6 x 100 = 600 + 4 x 101 = 4 x 10 = 40
+ 2 x 10º = 2 x 1 = 2 = 642 in base 10
This number is in base 10
The power indicates the position of the digit inside the
number
Positional Notation
7 7
dn * Rn-1 + dn-1 * Rn-2 + ... + d2 * R + d1
Formula:
642 is 63 * 102 + 42 * 10 + 21
R is the base of the number
n is the number of digits in the number
d is the digit in the ith position
in the number
Positional Notation reloaded
8 7
dn * Rn-1 + dn-1 * Rn-2 + ... + d2 * R + d1
… but, in CS, the digits are numbered from zero, to match the power of the base:
dn-1 * Rn-1 + dn-2 * Rn-2 + ... + d1 * R1 + d0 * R0
The text shows the digits numbered like this:
Positional Notation
9 6 8
What if 642 has the base of 13?
642 in base 13 is equal to 1068 in base 10 64213 = 106810
+ 6 x 132 = 6 x 169 = 1014 + 4 x 131 = 4 x 13 = 52 + 2 x 13º = 2 x 1 = 2
= 1068 in base 10
Positional Notation
10
In a given base R, the digits range from 0 up to R – 1
R itself cannot be a digit in base R
Trick problem:
Convert the number 473 from base 6 to base 10
Binary
11 9
Decimal is base 10 and has 10 digits:
0,1,2,3,4,5,6,7,8,9
Binary is base 2 and has 2 digits:
0,1
12
Converting Binary to Decimal
13
What is the decimal equivalent of the binary number 1101110?
11011102 = ???10
13
Converting Binary to Decimal
14
What is the decimal equivalent of the binary number 1101110?
1 x 26 = 1 x 64 = 64 + 1 x 25 = 1 x 32 = 32 + 0 x 24 = 0 x 16 = 0 + 1 x 23 = 1 x 8 = 8 + 1 x 22 = 1 x 4 = 4 + 1 x 21 = 1 x 2 = 2 + 0 x 2º = 0 x 1 = 0
= 110 in base 10
13
QUIZ:
10011010
2= ???
1015
Bases Higher than 10
16 10
How are digits in bases higher than 10 represented?
Base 16 (hexadecimal, a.k.a. hex) has 16 digits:
0,1,2,3,4,5,6,7,8,9,
A,B,C,D,E
,F
Converting Hexadecimal to Decimal
17
What is the decimal equivalent of the hexadecimal number DEF?
D x 162 = 13 x 256 = 3328 + E x 161 = 14 x 16 = 224 + F x 16º = 15 x 1 = 15
= 3567 in base 10
QUIZ:
2AF
16= ???
1018
Converting Octal to Decimal
19
What is the decimal equivalent of the octal number 642?
6428 = ???10
11
Converting Octal to Decimal
20
What is the decimal equivalent of the octal number 642?
6 x 82 = 6 x 64 = 384 + 4 x 81 = 4 x 8 = 32 + 2 x 8º = 2 x 1 = 2
= 418 in base 10
11
Are there any non-positional number systems?
Hint: Why did the Roman civilization have no contributions to mathematics?
21
Today we’ve covered pp.33-39 of the text (stopped before Arithmetic in Other Bases)
Solve in notebook for next class:
1, 2, 3, 4, 5, 20, 21, 22
22
QUIZ: Convert to decimal
1101 0011
2= ???
10AB7
16= ???
10513
8= ???
10692
8= ???
1023
Addition in Binary
24
Remember that there are only 2 digits in binary, 0 and 1
1 + 1 is 0 with a carry
Carry Values 0 1 1 1 1 1
1 0 1 0 1 1 1 +1 0 0 1 0 1 1 1 0 1 0 0 0 1 0
14
Addition QUIZ
25
Carry values go here
1 0 1 0 1 1 0 +1 0 0 0 0 1 1
14 Check in base ten!
Subtraction in Binary
26
Remember borrowing? Apply that concept here:
1 2 0 2 0 2
1 0 1 0 1 1 1 1 0 1 0 1 1 1 - 1 1 1 0 1 1 - 1 1 1 0 1 1 0 0 1 1 1 0 0 0 0 1 1 1 0 0
15 Borrow values
Check in base ten!
Subtraction QUIZ
27
1 0 1 1 0 0 0 - 1 1 0 1 1 1
15 Borrow values
Check in base ten!
Converting Decimal to Other Bases
28
While (the quotient is not zero)
Divide the decimal number by R
Make the remainder the next digit to the left in the answer
Replace the original decimal number with the quotient
Algorithm for converting number in base 10 to any other base R:
19
A.k.a. repeated division (by the base):
Converting Decimal to Binary
29
Example: Convert 17910 to binary 179 2 = 89 rem. 1
2 = 44 rem. 1
2 = 22 rem. 0
2 = 11 rem. 0
2 = 5 rem. 1
2 = 2 rem. 1
2 = 1 rem. 0
17910 = 101100112 2 = 0 rem. 1
Notes: The first bit obtained is the rightmost (a.k.a. LSB)
The algorithm stops when the quotient (not the remainder!) becomes zero
19
LSB MSB
Repeated division QUIZ
30
Convert 4210 to binary 42 2 = rem.
4210 = 2
19
The repeated division algorithm can be used to convert from any base into any other base, but we
use it only for 10 → 2
31
SKIP
Converting Decimal to Octal
Converting Decimal to Hex
Converting Binary to Octal
32
• Mark groups of three (from right)
• Convert each group
10101011 10 101 011 2 5 3
10101011 is 253 in base 8
17
Converting Binary to Hexadecimal
33
• Mark groups of four (from right)
• Convert each group
10101011 1010 1011 A B
10101011 is AB in base 16
18
Counting
34
Note the patterns!
Converting Octal to Hexadecimal
35
End-of-chapter ex. 25:
Explain how base 8 and base 16 are related
10 101 011 1010 1011 2 5 3 A B
253 in base 8 = AB in base 16
18
Binary Numbers and Computers
36
Computers have storage units called binary digits or bits
Low Voltage = 0
High Voltage = 1
All bits are either 0 or 1
22
Binary and Computers
37
Word= group of bits that the computer processes at a time
The number of bits in a word determines the word length of the computer. It is usually a multiple of 8.
1 Byte = 8 bits
• 8, 16, 32, 64-bit computers
• 128? 256?
23
Individual work
• Read Grace Hopper’s bio, the trivia frames, chapter review questions, and ethical issues
38
Who am I?
39
I wrote the world’s first compiler in 1952!
From the history of computing: bi-quinary
40
The front panel of the legendary IBM 650 IBM 650 (source: Wikipedia)
Roman abacus (source: MathDaily.com)
Chapter Review questions
• Describe positional notation
• Convert numbers in other bases to base 10
• Convert base-10 numbers to numbers in other bases
• Add and subtract in binary
• Convert between bases 2, 8, and 16 using groups of digits
• Count in binary
• Explain the importance to computing of bases that are powers of 2
41 24 6
Homework
Due next Friday, Feb. 3:
• End-of-chapter ex. 23, 26, 28, 29, 30, 31, 38
• End-of-chapter thought question 4 (paragraph-length answer required)
The latest homework assigned is always available on the course webpage
42