0% found this document useful (0 votes)
40 views

Integers and Algorithms: Dr. Kahrobaei City Tech (CUNY)

The document discusses different number systems like binary, hexadecimal, and their use in computer science. It also describes algorithms for manipulating numbers in these systems, including algorithms for addition, multiplication, division, modular exponentiation, and finding the greatest common divisor using the Euclidean algorithm. Examples are provided to demonstrate how to apply these algorithms to convert between number bases and perform operations like addition and division.

Uploaded by

Antony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Integers and Algorithms: Dr. Kahrobaei City Tech (CUNY)

The document discusses different number systems like binary, hexadecimal, and their use in computer science. It also describes algorithms for manipulating numbers in these systems, including algorithms for addition, multiplication, division, modular exponentiation, and finding the greatest common divisor using the Euclidean algorithm. Examples are provided to demonstrate how to apply these algorithms to convert between number bases and perform operations like addition and division.

Uploaded by

Antony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Integers and Algorithms

Dr. Kahrobaei
City Tech (CUNY)
Representation of Integers
Theorem: 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= ak bk + ak-1 bk-1 + + a1 b + a0
Where k is a nonegative integer, a0, a1, , ak are
nonnegative integers less than b, and ak 0.
Binary expansions
Choosing 2 as the base gives binary expansions
of integers. In binary notation each digit is either
a 0 or a 1.

Example: What is the decimal expansion of the


integer that has (1 0101 1111)2 as its binary
expansion?
(1 0101 1111)2 =1 28 + 0 27 +1 26 + 0 25 + 1 24
+ 1 23 + 1 22 + 1 21 + 1 2 0
Hexadecimal Expansions
Sixteen is another base used in computer
science. The base 16 expansion of an integer is
called its hexadecimal expansion. The
hexadecimal digits used are
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
where the letter A through F represent the digits
corresponding to the numbers 10 through 15 (in
decimal notation).
Hexadecimal Expansions
Example
Example: What is the decimal expansion of the
hexadecimal expansion of (2AE0B)16 ?
Solution:
(2AE0B)16 = 2164 + 10163 + 14162 + 0 16 + 11 =
(175627)10
Each hexadecimal digit can be represented using
four bits. (1110 0101)2 = (E5)16
because (1110)2 = (E)16 and (0101)2 = (5)16
Bytes, which are bit strings of length eight, can be
represented by two hexadecimal digits.
Algorithm: Constructing Base b
Expansions
Procedure base b expansion(n: positive integer)
q:=n
k:=0
While q 0
begin
ak := q mod b
q:= b q/b c
k:= k+1
end {the base b expansion of n is (ak-1 a1 a0 )b }
Find the binary expansion of (241)10
241= 2 . 120 +1
Successively dividing quotients by 2 gives
120 =2 . 60 +0
60 = 2 . 30 +0
30 = 2 . 15 + 0
15 = 2 . 7 +1
7 = 2 .3 +1
3= 2 . 1 + 1
1= 2 . 0 +1
(241)10 = (1111 0001)2
Algorithm Addition of Integers
Procedure add(a, b: positive integers)
{the binary expansions of a and b are (an-1 an-2 a1 a0 )2 and
(bn-1 bn-2 b1 b0 )2 , respectively}
c:=0
for j:=0 to n-1
begin
d:= b (aj + bj +c)/2 c
sj := aj + bj +c -2d
c:=d
end
sn :=c
{the binary expansion of the sum is (sn sn-1 s1 s0 )2
Algorithm Multiplying Integers
Procedure multiply(a, b: positive integers)
{the binary expansions of a and b are (an-1 an-2 a1 a0 )2 and (bn-1 bn-2
b1 b0 )2 , respectively}
for j:=0 to n-1
begin
if bj =1 then cj :=a shifted j places
else cj :=0
end
{c0, c1, , cn-1 are partial products}
p:=0
For j:=0 to n-1
p:=p+cj
{p is the value of ab}
03-6-002.jpg
Algorithm Computing div and mod
Procedure division algorithm(a: integer, b: positive integer)
q:=0
r:=|a|
While r >=d
Begin
r:=r-d
q:=q+1
End
If a<0 and r>0 then
Begin
r:=d-r
q:=-(q+1)
End
{q=a div d is the quotient, r=a mod d is the remainder}
Algorithm Modular Exponentiation
procedure modular exponentiation (b: integer, n=
(ak-1 ak-2 a1 a0 )2 , m: positive integers)
x:=1
power := b mod m
for i :=0 to k-1
begin
if ai =1 then x:= (x . Power) mod m
power := (power.power) mod m
end
{x equals bn mod m}
The Euclidean Algorithm
Lemma: is a =bq+r where a, b, q and r are
integers. Then gcd(a, b)= gcd(b, r)

Algorithm The Euclidean Algorithm


Procedure gcd(a, b : positive integers)
x:= a
y:= b
While y 0
Begin
r:=x mod y
x:=y
y:=r
End{gcd(a,b) is x}
Example
Find the greatest common divisor of 414 and
662 using the Euclidean algorithm.
Successive uses of the division algorithm give:
662=414 . 1+248
414=248 . 1 +166
248= 166 . 1+82
166 = 82 . 2 + 2
82=2 . 41

You might also like