Title: Primarily interested in base 2, 10, and 16
1Number Formats
Primarily interested in base 2, 10, and 16
Binary
Decimal
Hexadecimal
2Number Formats Binary
- Binary means base 2
- Each digit is either a 1 or a 0
- Example below 1 x 23 0 x 22 1 x 21 1 x 20
- The largest number possible in N digits is 2N -
1
1
1
1
0
3Number Formats Binary
Base 10 Base 2 0 0000 1 0001 2
0010 3 0011 4 0100 5 0101
6 0110 7 0111 8 1000 9
1001 10 1010 11 1011 12 1100 13
1101 14 1110 15 1111 16 10000
4Number Formats Convert Binary to Decimal
Binary
Decimal
Hex
5Number Formats Convert Binary to Decimal
1 x 27 1 x 26 0 x 25 1 x 24 1 x 23 0 x
22 1 x 21 1 x 20 128 64 0 16 8
0 2 1 21910 (The subscript indicates the
base used to represent the number)
1
1
1
1
1
1
0
0
6Number Formats Binary Your Turn
Convert the following numbers to base 10 a.
11 b. 101 c. 1101 d. 10110110 e. 110110110
7Number Formats Convert Decimal to Binary
Binary
Decimal
Hex
8Number Formats Convert Decimal to Binary
To Base 2 Divide by 2 until no longer
possible then read the remainders from top to
bottom. Example 3710 0 r 1 37 Base
10 ??? Base 2 2 1 r 0
2 2 r 0
2 4 r 1
2 9 r 0
2 18 r 1
2
37 37 Base 10 100101 Base 2 1 x 25 0 x 24
0 x 23 1 x 22 0 x 21 1 x 20
9Number Formats Convert Decimal to Binary -
Alternative Method
-Same process, going from top to
bottom -Conversion 7310 -- To Binary 73/2 36
with 1 remainder. 36/2 18 with 0
remainder. 18/2 9 with 0 remainder. 9/2 4
with 1 remainder. 4/2 2 with 0
remainder. 2/21 with 0 remainder. 1/20
with 1 remainder.
(read from bottom up)
10010012
10Number Formats Your Turn
Convert the following numbers to base 2 a.
14 b. 31 c. 104 d. 327 e. 1023
11Number Formats Hexadecimal
Base 10 Base 16 Base 2 0 0 0000 1
1 0001 2 2 0010 3 3
0011 4 4 0100 5 5 0101 6
6 0110 7 7 0111 8
8 1000 9 9 1001 10 A
1010 11 B 1011 12 C 1100 13
D 1101 14 E 1110 15 F
1111 16 10 10000
12Number Formats Hexadecimal
- Base 16 A2716 ??
- 10 x 162 2 x 161 7 x 160
- 10 x 256 2 x 16 7 x 1 259910
Base 10 Base 16 Base 2 0 0 0000
10 A 1010 11 B 1011 12
C 1100 13 D 1101 14 E
1110 15 F 1111 16 10 10000
2
A
7
13Number Formats Convert Hexadecimal to Decimal
Binary
Decimal
Hex
14Number Formats Your Turn
Convert the following numbers to base 10 a.
14 b. 3F1 c. 10C4 d. ABCD e. ABCDEF1
15Number Formats Convert Decimal to Hexadecimal
Binary
Decimal
Hex
16Number Formats Convert Decimal to Hexadecimal
To Hexadecimal Divide by 16 until no longer
possible then read the remainders from top to
bottom. Example 37010 0 r1 370
Base 10 ??? Base 16 16 1 r 7
16 23 r 2
16 370 370
Base 10 172 Base 16 1 x
162 7 x 161 2 x 160
17Number Formats Convert Decimal to Hex
-- Convert 7310 To HEX 73/16 4 with 9
remainder. 4/16 0 with 4 remainder.
4916
18Number Formats Your Turn
Convert the following numbers to base 16 a.
14 b. 31 c. 104 d. 327 e. 1023
19Number Formats Convert Binary to Hexadecimal
Binary
Decimal
Hex
20Number Formats Convert Binary to Hexadecimal
Binary To Hex Group by 4s
99 10A 1101 1011 11B 13
11 12C 13D D B
DB16 14E 15F
21Number Formats Your Turn
Convert the following numbers to base 16 a.
11 b. 101 c. 1101 d. 10110110 e.
1101101101101101
22Number Formats Convert Hexadecimal to Binary
Binary
Decimal
Hex
23Number Formats Convert Hexadecimal to Binary
-Hex (base 16) -Conversion 9A16 -- To
Decimal 9161 10160 144 10 15410 --
To Binary 9 A 100110102
1001 1010
24Number Formats Your Turn
Convert the following numbers to base 2 a.
1F b. 3C c. 1D4 d. 32E e. 1A23
25The Euclidean Algorithm
- The Euclidean Algorithm finds the greatest common
divisor of two integers a and b. - For example, if we want to find gcd(287, 91), we
divide 287 (the larger number) by 91 (the smaller
one) - 287 91?3 14
- 287 - 91?3 14
- 287 91?(-3) 14
- We know that for integers a, b and c,if a b,
then a bc for all integers c. - Therefore, any divisor of 91 is also a divisor of
91?(-3).
26The Euclidean Algorithm
- 287 91?(-3) 14
- We also know that for integers a, b and c,
- if a b and a c, then a (b c).
- Therefore, any divisor of 287 and 91 must also be
a divisor of 287 91?(-3), which is 14. - Consequently, the greatest common divisor of 287
and 91 must be the same as the greatest common
divisor of 14 and 91 - gcd(287, 91) gcd(14, 91).
27The Euclidean Algorithm
- In the next step, we divide 91 by 14
- 91 14?6 7
- This means that gcd(14, 91) gcd(14, 7).
- So we divide 14 by 7
- 14 7?2 0
- We find that 7 14, and thus gcd(14, 7) 7.
- Therefore, gcd(287, 91) 7.
28The Euclidean Algorithm
- In pseudocode, the algorithm can be implemented
as follows - procedure gcd(a, b positive integers)
- x a
- y b
- while y ? 0
- begin
- r x mod y
- x y
- y r
- end x is gcd(a, b)
29Representations of Integers
- Let b be a positive integer greater than 1.Then
if n is a positive integer, it can be expressed
uniquely in the form - n akbk ak-1bk-1 a1b a0,
- where k is a nonnegative integer,
- a0, a1, , ak are nonnegative integers less than
b, - and ak ? 0.
- Example for b10
- 859 8?102 5?101 9?100
30Representations of Integers
- Example for b2 (binary expansion)
- (10110)2 1?24 1?22 1?21 (22)10
- Example for b16 (hexadecimal expansion)
- (we use letters A to F to indicate numbers 10 to
15) - (3A0F)16 3?163 10?162 15?160 (14863)10
-
31Representations of Integers
- How can we construct the base b expansion of an
integer n? - First, divide n by b to obtain a quotient q0 and
remainder a0, that is, - n bq0 a0, where 0 ? a0 lt b.
- The remainder a0 is the rightmost digit in the
base b expansion of n. - Next, divide q0 by b to obtain
- q0 bq1 a1, where 0 ? a1 lt b.
- a1 is the second digit from the right in the base
b expansion of n. Continue this process until you
obtain a quotient equal to zero.
32Representations of Integers
- Example What is the base 8 expansion of
(12345)10 ? - First, divide 12345 by 8
- 12345 8?1543 1
- 1543 8?192 7
- 192 8?24 0
- 24 8?3 0
- 3 8?0 3
- The result is (12345)10 (30071)8.
33Representations of Integers
- procedure base_b_expansion(n, b positive
integers) - q n
- k 0
- while q ? 0
- begin
- ak q mod b
- q ?q/b?
- k k 1
- end
- the base b expansion of n is (ak-1 a1a0)b
34Addition of Integers
- How do we (humans) add two integers?
- Example 7583 4932
carry
1
1
1
5
1
5
2
1
carry
1
1
Binary expansions (1011)2
(1010)2
1
0
1
0
1
35Addition of Integers
- Let a (an-1an-2a1a0)2, b (bn-1bn-2b1b0)2.
- How can we algorithmically add these two binary
numbers? - First, add their rightmost bits
- a0 b0 c0?2 s0,
- where s0 is the rightmost bit in the binary
expansion of a b, and c0 is the carry. - Then, add the next pair of bits and the carry
- a1 b1 c0 c1?2 s1,
- where s1 is the next bit in the binary expansion
of a b, and c1 is the carry.
36Addition of Integers
- Continue this process until you obtain cn-1.
- The leading bit of the sum is sn cn-1.
- The result is
- a b (snsn-1s1s0)2
37Addition of Integers
- Example
- Add a (1110)2 and b (1011)2.
- a0 b0 0 1 0?2 1, so that c0 0 and s0
1. - a1 b1 c0 1 1 0 1?2 0, so c1 1 and
s1 0. - a2 b2 c1 1 0 1 1?2 0, so c2 1 and
s2 0. - a3 b3 c2 1 1 1 1?2 1, so c3 1 and
s3 1. - s4 c3 1.
- Therefore, s a b (11001)2.
38Addition of Integers
- procedure add(a, b positive integers)
- c 0
- for j 0 to n-1
- begin
- d ?(aj bj c)/2?
- sj aj bj c 2d
- c d
- end
- sn c
- the binary expansion of the sum is
(snsn-1s1s0)2