You are on page 1of 21

Discrete Mathematics I

(ITT 102)
Unit 1
1’s & 2’s Complements
Lecturer: Cecil White
Euclidean Algorithm
Modular Arithmetic
UNIVERSITY COLLEGE OF THE CARIBBEAN
Content
• In this section we will look at:
• 1’s & 2’s Complements
• Euclidean Algorithm
• Modular Arithmetic

Unit 1: 1 and 2 Complements 2


One’s Complement
• Binary Number System is one the type of most popular Number
Representation techniques that used in digital systems.
• In the Binary System, there are only two symbols or possible digit
values, i.e., 0 (off) and 1 (on).
• Represented by any device that only 2 operating states or possible
conditions.

Unit 1: 1 and 2 Complements 3


One’s Complement (Cont.)

• Generally, there are two types of complement of Binary number: 1’s


complement and 2’s complement.
• The ones' complement of a binary number is the value obtained by
inverting all the bits in the binary representation of the number
(swapping 0s and 1s).
• To get 1’s complement of a binary number, simply invert the given
number.
• Example:
o 1’s complement of binary number 110010 is 001101.

Unit 1: 1 and 2 Complements 4


One’s Complement (Cont.)

• This mathematical operation is primarily of interest in computer


science, where it has varying effects depending on how a specific
computer represents numbers.

Unit 1: 1 and 2 Complements 5


Two’s Complement
• Two's complement is a mathematical operation on binary numbers,
and is an example of a radix complement.
• It is used in computing as a method of signed number representation.
The two's complement of an N-bit number is defined as its
complement with respect to 2; the sum of a number and its two's
complement is 2.
• For instance, for the three-bit number 010₂, the two's complement is
110₂, because 010₂ + 110₂ = 1000₂ = 8₁₀ which is equal to 2.

Unit 1: 1 and 2 Complements 6


Two’s Complement (Cont.)
• There is a simple algorithm to convert a binary number into 2’s
complement.
• To get 2’s complement of a binary number, simply invert the given
number and add 1 to the least significant bit (LSB) of given result.
• Implementation of 4-bit 2’s complementation number is given as
following below and on the next slide.
• Example-1 − Find 2’s complement of binary number 10101110.
o Simply invert each bit of given binary number, which will be

01010001.
o Then add 1 to the LSB of this result, i.e., 01010001+1=01010010

which is answer.

Unit 1: 1 and 2 Complements 7


Two’s Complement (Cont.)
• Example-2 − Find 2’s complement of binary number 10001.001.
o Simply invert each bit of given binary number, which will be

01110.110.
o Then add 1 to the LSB of this result, i.e., 01110.110+1=01110.111

which is answer.
• Example-3 − Find 2’s complement of each 3 bit binary number.
o Simply invert each bit of given binary number, then add 1 to LSB of

these inverted numbers.


o So 000 becomes 111 (1’s complement) which then becomes 000 in

2’s complement.
o See full output on next slide

Unit 1: 1 and 2 Complements 8


Two’s Complement (Cont.)
Binary 1’s Complement 2’s Complement
000 111 000
001 110 111
010 101 110
011 100 101
100 011 100
101 010 011
110 001 010
111 000 001

Unit 1: 1 and 2 Complements 9


Euclidean Algorithm
• The least common multiple (LCM) of two integers is the smallest
positive integer that is a multiple of both.
• The greatest common divisor (GCD) of two integers is the largest
positive integer dividing both.
• The product of the two numbers is the product of the LCM and the
GCD.

Unit 1: 1 and 2 Complements 10


Euclidean Algorithm(Cont.)
• In mathematics the Euclidean algorithm or Euclid's algorithm is an
efficient method for computing the greatest common divisor of two
integers the largest number that divides them both without a
remainder.
• It is named after the ancient Greek mathematician Euclid who first
described it in his Elements.
• The Euclidean algorithm is a way to find the greatest common divisor
(GCD) of two positive integers a and b.
• First let me show the computations for a = 210 and b = 45.

Unit 1: 1 and 2 Complements 11


Euclidean Algorithm(Cont.)
• Divide 210 by 45 and get the result 4 with remainder 30, therefore
210 = 4•45 + 30.
• Divide 45 by 30 and get the result 1 with remainder 15, therefore
45 =1•30 +15.
• Therefore the gcd(45, 30) = gcd(30, 15) and also
• Divide 40 by 15 we get the result 3 with remainder 0, therefore
45 = 3 •15 + 0 then 3 is a multiple and the gcd(210, 45)

Unit 1: 1 and 2 Complements 12


Euclidean Algorithm(Cont.)
Another Example
• How do you find the greatest common divisor (gcd) of two integers a,
b?
• We denote the greatest common divisor of a and b by gcd(a b) or
sometimes even just (a, b).
• If (a, b) = 1 we say a and b are coprime.
• The obvious answer is to list all the divisors a and b and look for the
greatest one they have in common.
• However this requires a and b to be factorized and no one knows how
to do this efficiently.

Unit 1: 1 and 2 Complements 13


Euclidean Algorithm(Cont.)
• A few simple observations lead to a far superior method: Euclid’s
algorithm or the Euclidean algorithm.
• First if d divides a and d divides b then d divides their difference a - b
where a is the larger of the two.
• But this means we have shrunk the original problem: now we just
need to find gcd(a, a − b).
• We repeat until we reach a trivial case.
• Hence we can find gcd(a, b) by doing something that most people
learn in primary school: division and remainder.

Unit 1: 1 and 2 Complements 14


Euclidean Algorithm(Cont.)
• Suppose we wish to compute gcd(27, 33).
o First we divide the bigger one by the smaller one:

o 33 = 1 × 27 + 6

o Thus gcd(33, 27) = gcd(27, 6). Repeating:

o 27 = 4 × 6 + 3 and we see gcd(27, 6) = gcd(6, 3).

o Lastly 6 = 2 × 3 + 0

o Since 6 is a perfect multiple of 3 therefore the gcd(6, 3) = 3 and we

have found that gcd(33, 27) = 3.

Unit 1: 1 and 2 Complements 15


Modular Arithmetic
• Every time you think about “time,” you use modular arithmetic
because it deals with cycles of integers and remainders just like a
clock.
• For example, suppose your clock reads 9:00 (am/pm is not
important).
• What will the clock show in 10 hours?
• Well, 9 + 10 = 19, but “19 o’clock” is not something that can be
displayed on a clock with the numbers 1 to 12.

Unit 1: 1 and 2 Complements 16


Modular Arithmetic (Cont.)
• So, what do we do?
• We subtract 12 from 19 and proudly say that the clock will show 7:00.
• This is the idea behind modular arithmetic, which is sometimes
referred to as “clock arithmetic” because 19 mod 12 = 7 mod 12,
where 7 represents the remainder when 19 is divided by 12.
• How To Do Modular Arithmetic?
• This means that modular arithmetic finds the remainder of a number
upon division.

Unit 1: 1 and 2 Complements 17


Modular Arithmetic (Cont.)
• Example 1:
o What is 16 mod 12?
o Well 16 divided by 12 equals 1 remainder 4. So the answer is 4!
• Example 2:
o What about 15 mod 2?
o Here, 15 divided by 2 equals 7 remainder 1, so the solution is 1!
• Example 3:
o And if you have 18 mod 9?
o We know that 18 divided by 9 equals 2 remainder 0, so that means 18 mod 9 is
equivalent to 0!

Unit 1: 1 and 2 Complements 18


Modular Arithmetic (Cont.)
• The trick for modular arithmetic is to focus on the remainder
• However, the remainder must be positive.
• Example #4
o For this problem, suppose we wanted to evaluate -97 mod 11.
o -97 divided by 11 equals -8 remainder -9.
o But since this remainder is negative, we have to increase our quotient by 2 to
say -97 divided by 11 equals -9 remainder 2, as 11(-9) + 2 = -97
o Therefore, -97 mod 11 equals 2

Unit 1: 1 and 2 Complements 19


Modular Congruence
• Now, in number theory, we often want to focus on whether two
integers say a and b, have the same remainder when divided by m.
• This is the idea behind modular congruence.
• Congruence is nothing more than a statement about divisibility and
was first introduced by Carl Friederich Gauss.
• He surmised that if a and b are integers, and m is a positive integer,
then a is congruent to b modulo m if and only if m divides a minus b.
• In other words, a congruence modulo asks whether or not a and b are
in the same equivalence class.

Unit 1: 1 and 2 Complements 20


Modular Congruence

Unit 1: 1 and 2 Complements 21

You might also like