Lecture 4 2
Lecture 4 2
Section 4.2
Anna-Simone Frank1
MNF130
Spring 2024
1
Slides created by Tom Michoel and modified by Erik Maartensson and
Anna-Simone Frank
Integer representations
Definition
If b > 1 and n > 0 are integers, then the base b expansion of n is the
unique expression of n in the form
n = ak b k + ak−1 b k−1 + · · · + a1 b + a0
Example
I The usual decimal expansion is obtained by setting the base
b = 10. Here the choice of basis is normally implied.
I The binary expansion of an integer is obtained by setting the base
b = 2. Then each digit ai is either 0 or 1.
I The octal and hexadecimal expansion are obtained by setting the
base b = 8 and 16, respectively.
4
Example
The decimal representation of the integer with binary expansion
(101100011)2 is:
(10110011)2
= 1 · 27 + 0 · 26 + 1 · 25 + 1 · 24 + 0 · 23 + 0 · 22 + 1 · 21 + 1 · 20
= 128 + 32 + 16 + 2 + 1
= 179
5
Example
The binary representation of the integer 67 (decimal representation) is:
67 mod 2 = 1 67 div 2 = 33
33 mod 2 = 1 33 div 2 = 16
16 mod 2 = 0 16 div 2 = 8
8 mod 2 = 0 8 div 2 = 4
4 mod 2 = 0 4 div 2 = 2
2 mod 2 = 0 2 div 2 = 1
1 mod 2 = 1 1 div 2 = 0
Hence 67 = (1000011)2
6
Decimal from and to octal
(1337)8 = 1 · 83 + 3 · 82 + 3 · 8 + 7 = 597
Conversely: given 1337 in decimal representation.
7
Hexadecimal representation
8
Decimal from and to hexadecimal
9
Binary to and from octal
10
Binary to and from hexadecimal
11
The general algorithm for constructing Base b expansions is:
procedure base b expansion(b > 1, n : integers)
q := n
k := 0
while q 6= 0 do
ak := q mod b
q := q div b
k := k + 1
end while
return (ak−1 , . . . , a1 , a0 )
end procedure
12
Addition of integers in binary notation
I Follow the usual procedure for decimal representations!
I In pseudocode:
procedure Add(a = (an−1 . . . a1 a0 )2 , b = (bn−1 . . . b1 b0 )2 )
c := 0
for j := 0, . . . , n − 1 do
sj := (aj + bj + c) mod 2
c := (aj + bj + c) div 2
end for
sn := c
return (sn . . . s1 s0 )2 binary expansion of a + b
end procedure
13
Multiplication of integers in binary notation
I Follow the usual procedure for decimal representations!
14
I For a = (an−1 . . . a1 a0 )2 , b = (bn−1 . . . b1 b0 )2 , this can be used as
follows:
15
I In pseudocode:
procedure Multiply(a = (an−1 . . . a1 a0 )2 , b = (bn−1 . . . b1 b0 )2 )
for j := 0, . . . , n − 1 do
if bj = 1 then
cj := (an−1 . . . a1 a0 |0 .{z
. . 0})2
j×
else
cj := 0
end if
end for
p := 0
for j := 0, . . . , n − 1 do
p := add(p, cj )
end for
return p binary expansion of ab
end procedure
16
Complexity of binary addition and multiplication
2 https://en.wikipedia.org/wiki/Multiplication_algorithm#
Computational_complexity_of_multiplication 17
Modular exponentiation
n = ak−1 2k−1 + · · · + a1 2 + a0
and write
k−1
b n = b ak−1 2 . . . b a1 2 b a0
18
I Then we use the previous result
I Hence
k−1
b n mod m = (b ak−1 2 mod m) . . . (b a1 2 mod m)(b a0 mod m) mod m
19
Example
What is 367 mod 7?
I Apply the previous result:
3 mod 7 = 3
32 mod 7 = 9 mod 7 = 2
34 mod 7 = (32 mod 7)(32 mod 7) mod 7 = 4
38 mod 7 = (34 mod 7)(34 mod 7) mod 7 = 2
316 mod 7 = (38 mod 7)(38 mod 7) mod 7 = 4
332 mod 7 = (316 mod 7)(316 mod 7) mod 7 = 2
364 mod 7 = (332 mod 7)(332 mod 7) mod 7 = 4
I Since 67 = (1000011)2 = 26 + 2 + 1 = 64 + 2 + 1
20
I In pseudocode:
procedure Modular Exponentiation(b : integer,
n = (an−1 . . . a1 a0 )2 , m : positive integers)
x := 1
power := b mod m
for j := 0, . . . , n − 1 do
if aj = 1 then
x := (x · power) mod m
end if
power := (power · power) mod m
end for
return x = b n mod m
end procedure
21
On optimal exponentiation
3 https://en.wikipedia.org/wiki/Addition-chain_exponentiation
22