0% found this document useful (0 votes)
9 views20 pages

Lecture 4 2

The document discusses different number systems and algorithms for integer representation and arithmetic. It defines base b expansions and provides examples of decimal, binary, octal and hexadecimal representations. It then describes algorithms for converting between bases and for addition and multiplication in binary. It concludes with a discussion of modular exponentiation.

Uploaded by

hallertjohn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Lecture 4 2

The document discusses different number systems and algorithms for integer representation and arithmetic. It defines base b expansions and provides examples of decimal, binary, octal and hexadecimal representations. It then describes algorithms for converting between bases and for addition and multiplication in binary. It concludes with a discussion of modular exponentiation.

Uploaded by

hallertjohn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Integer representations and Algorithms – Rosen

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

where k is a nonnegative integer, a0 , a1 , . . . , ak are nonnegative integers


less than b, and ak 6= 0. We write n = (ak , ak−1 , . . . , a1 , a0 )b

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.

1337 mod 8 = 1 1337 div 8 = 167


167 mod 8 = 7 167 div 8 = 20
20 mod 8 = 4 20 div 8 = 2
2 mod 8 = 2 2 div 8 = 0

Thus 1337 = (2471)8

7
Hexadecimal representation

In hexadecimal representation we let b = 16 and


k
X
n= ai 16i , (1)
i=0

where ai ∈ {0, 1, . . . , 15}.


We normally only have 10 symbols to denote digits with, so in
hexadecimal we let A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
We write 10 · 162 + 5 · 16 + 15 = (A5F )16 , for example.

8
Decimal from and to hexadecimal

(1337)16 = 1 · 163 + 3 · 162 + 3 · 16 + 7 = 4919.


Conversely: Given 421337 in decimal representation

421337 mod 16 = 9 421337 div 16 = 26333


26333 mod 16 = 13 26333 div 16 = 1645
1645 mod 16 = 13 1645 div 16 = 102
102 mod 16 = 6 102 div 16 = 6
6 mod 16 = 6 6 div 16 = 0

Thus 421337 = (66DD9)16 .

9
Binary to and from octal

Conversion between binary and octal representations is easy!


I Binary to octal:
I Given (11, 110, 101, 100)2 .
I Zero-pad the number so the length becomes a multiple of 3:
((0)11, 110, 101, 100)2 .
I Translate 3 bits at a time: ((0)11, 110, 101, 100)2 = (3654)8 .
I Octal to binary:
I Given (3654)8 .
I Translate each digit to 3 bits: (3654)8 = ((0)11, 110, 101, 100)2
I Remove the starting zero(s):
((0)11, 110, 101, 100)2 → (11, 110, 101, 100)2

10
Binary to and from hexadecimal

Conversion between binary and hexadecimal representations is easy!


I Binary to hexadecimal:
I Given (111, 1010, 1100)2 .
I Zero-pad the number so the length becomes a multiple of 4:
((0)111, 1010, 1100)2 .
I Translate 4 bits at a time: ((0)111, 1010, 1100)2 = (7AC )16 .
I Hexadecimal to binary:
I Given (7AC )16 .
I Translate each digit to 4 bits: (7AC )16 = ((0)111, 1010, 1100)2
I Remove the starting zero(s):
((0)11, 110, 101, 100)2 → (11, 110, 101, 100)2

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!

Figure 1: Addition of binary integers

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!

Figure 2: Multiplication of binary integers

I Let a = (an−1 . . . a1 a0 )2 . First note that for all j

a2j = (a0 20 + a1 21 + · · · + an−1 2n−1 )2j


= a0 2j + a1 2j+1 + · · · + an−1 2j+n−1
= (an−1 . . . a1 a0 0| .{z
. . 0})2

Hence multiplying a by a power of 2 is the same as padding extra


zeros at the tail of the binary expansion.

14
I For a = (an−1 . . . a1 a0 )2 , b = (bn−1 . . . b1 b0 )2 , this can be used as
follows:

ab = a(b0 20 + b1 21 + · · · + bn−1 2n−1 )


= (ab0 )20 + (ab1 )21 + · · · + (abn−1 )2n−1

I If bj = 0, abj = 0. If bj = 1, abj = a. Hence,


X X
ab = a2j = (an−1 . . . a1 a0 0| .{z
. . 0})2 ,
{j : bj 6=0} {j : bj 6=0} j×
P
where the second means using the addition algorithm for binary
expansions.

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

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

I Adding two n-bit numbers takes O(n) bit operations.


I Multiplying two n-bit numbers means adding n-bit numbers n − 1
times, giving an O(n2 ) algorithm.
I For smaller numbers, on modern computers adding and multiplying
normal-sized numbers take roughly the same time.
I In cryptography we sometimes work with huge numbers, where
the difference between addition and multiplication is really
noticeable.
I Perhaps surpricingly, it’s possible to multiply two n-bit numbers in as
low as O(n log(n)).2

2 https://en.wikipedia.org/wiki/Multiplication_algorithm#

Computational_complexity_of_multiplication 17
Modular exponentiation

I In cryptography we need to be able to compute b n mod m where n


can be very large. Even if n is not large, b n quickly becomes
huge. For instance
367 = 9.2709 × 1031

I Instead of computing b n and then taking the mod operation, we


express n in its binary expansion

n = ak−1 2k−1 + · · · + a1 2 + a0

and write
k−1
b n = b ak−1 2 . . . b a1 2 b a0

where each aj is either 0 or 1.

18
I Then we use the previous result

(ab) mod m = (a mod m)(b mod m) mod m

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

I Each of these factors can be computed from the previous one:


j j−1 j−1
b 2 mod m = (b 2 mod m)(b 2 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

367 mod 7 = (364 32 3 mod 7) = (4 · 2 · 3) mod 7 = 3

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

Given the task to calculate b 15 . With the binary approach we need to


calculate b 2 , b 4 = (b 2 )2 , b 8 = (b 4 )2 and finally b 15 = b · b 2 · b 4 · b 8 - 6
multiplications in total.
However, we can also calculate b 2 , b 3 = b 2 · b, b 6 = (b 3 )2 , b 9 = b 3 · b 6
and finally b 15 = b 6 · b 9 - 5 multiplications in total!
15 is the smallest exponent where the binary algorithm is not optimal.
Finding the optimal method in general is very hard3 .

3 https://en.wikipedia.org/wiki/Addition-chain_exponentiation
22

You might also like