You are on page 1of 110

DIGITAL SYSTEMS

AND BINARY
NUMBERS

SCHOOL OF COMPUTING RIEL A. GOMEZ


BASIC INFORMATION

• Professor's Name: Riel A. Gomez

• Office: 8th Floor (Faculty Room)

• Email:
riel.gomez@iacademy.edu.ph
Introduction
BASIC INFORMATION

• Absences

– A student will be given a grade of FA (failure due to


absences) for a subject if he/she is absent for more
than three (3) times (for classes that meet once a
week).

– One late will be considered as a half-absence.

– A student who leaves the class before the end of the


class period will be considered as absent for that
meeting.

Introduction
BASIC INFORMATION
• The following rules will be strictly enforced during
classes:

‒ Cell phones, tablets, and other gadgets are not


allowed while class is in session. They must be kept
hidden.

‒ 100% attention is required from you. Direct questions


to the instructor, not the seatmate.

‒ No sleeping in class.

• You may eat and drink while in class but observe


cleanliness.
Introduction
BASIC INFORMATION

• Class Requirements : 4 or 5 Major


Exams. Final grade is just the
average of the major exams.

• Cancel the lowest major exam.

• A difficulty factor is given for each


exam (to raise the score).
Introduction
BASIC INFORMATION
• If you missed an exam:
‒ you have one (1) week to take it (except for the
last exam)
‒ send me an email to schedule your make-up
exam
‒ no advanced exams will be given
‒ there will be no difficulty factor for missed
exams.

• Passing is 70%. Absolutely no extra work will be


given for those who will fail or those who want a
higher grade.
Introduction
ANALOG VERSUS DIGITAL

• Analog Signal
+5v

An analog signal is a
continuous signal that 0v
can assume all possible
values (infinite) within -5v
its given range.

Digital Systems and Binary Numbers


ANALOG VERSUS DIGITAL

• Discrete Signal +5v

A discrete signal is a 0v
signal that can assume
only certain values
within its given range. -5v

Digital Systems and Binary Numbers


ANALOG VERSUS DIGITAL

Digital Systems and Binary Numbers


ANALOG VERSUS DIGITAL

• Digital Signal high, on, or 1

A digital signal is a
discrete signal that can +5v
assume only two
possible values (binary). 0v

One value is referred to


as high, on, or 1 while low, off, or 0
the other value is low,
off, or 0.

Digital Systems and Binary Numbers


ANALOG VERSUS DIGITAL
• The main advantage of digital signals over analog signals is that
the precise signal level of the digital signal is not vital.

This means that digital signals are fairly immune to the


imperfections (noise) of real electronic systems which tend to
distort analog signals.

Digital Systems and Binary Numbers


NUMBER SYSTEMS
• A number system is a way of representing numbers.

• The base-10 number system (also called the decimal system) is


employed in everyday arithmetic to represent numbers by strings
of digits.

• The decimal system uses 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9).

• Depending on its position in the string, each digit has an


associated value of an integer raised to the power of 10.

• For example, the decimal number 7245 is interpreted to represent


7 thousands plus 2 hundreds plus 4 tens plus 5 units.

7245 = (7
(7××1000)
1000) + (2
(2××100)
100) + (4
(4××10)
10) ++ (5
(5××1)1)

Digital Systems and Binary Numbers


NUMBER SYSTEMS

• The thousands, hundreds, tens, and units are


powers of 10 implied by the position of the digits
(positional notation).

• The value of the number is computed as follows:


position of each
number
3 2 1 0
7245 = (7×1000) + (2×100) + (4×10) + (5×1)

(7×103)3) ++(2×10
= (7×10 2)2 + (4×101)1
(2×10 (5×100)0)
) + (4×10 ) + (5×10

Digital Systems and Binary Numbers


NUMBER SYSTEMS

• Other Examples:
4 3 2 1 0

79561 = 7 ××10
104 4 + 9 × 103 + 5 × 1022 + 6 × 1011++11××10
1000
2 1 0

384 = 3 × 102 + 8 × 101 + 4 × 100


3 2 1 0

6285 = 6 × 103 + 2 × 102 + 8 × 101 + 5 × 100

Digital Systems and Binary Numbers


NUMBER SYSTEMS

• The positional notation is also applicable to


numbers with fractional components.

For example:
1 0 -1 -2

72.45 = 7 × 1011 + +2 2× ×1010


0+ ×-110+-15 ×+ 10
0 4+×410 5 -2× 10-2

= 7 ×× 10
10 ++ 22××11 ++ 44××0.1
0.1 + 55 ××0.01
0.01
= 70 + 2 + 0.4 + 0.05

Digital Systems and Binary Numbers


NUMBER SYSTEMS

• The convention is to write only the digits and infer the


corresponding powers of 10 from their positions.

• In general, a decimal number with n digits to the left of the


decimal point and m digits to the right of the decimal point
is represented by a string of coefficients:

An-1 An-2 … A1 A0 . A-1 A-2 … A-m+1 A-m

• Each coefficient Ai is one of 10 digits (0, 1, 2, 3, …, 9). The


subscript value i gives the position of the coefficient and,
hence, the weight 10i by which the coefficient must be
multiplied.

Digital Systems and Binary Numbers


NUMBER SYSTEMS
• The decimal number system is said to be of base 10 or radix 10, because the
coefficients are multiplied by powers of 10 and the system uses 10 distinct digits
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).

• The following are just some of the different number systems:


System Base Digits
Binary 2 0, 1
Tertiary/Ternary 3 0, 1, 2
Quaternary 4 0, 1, 2, 3
Quinary 5 0, 1, 2, 3, 4
Senary 6 0, 1, 2, 3, 4, 5
Septenary 7 0, 1, 2, 3, 4, 5, 6
Octal 8 0, 1, 2, 3, 4, 5, 6, 7
Nonary 9 0, 1, 2, 3, 4, 5, 6, 7, 8
Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Digital Systems and Binary Numbers


NUMBER SYSTEMS

System Base Digits


Undecimal 11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A
Duodecimal 12 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B
Tridecimal 13 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C
Tetradecimal 14 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D
Pentadecimal 15 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E
Hexadecimal 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

• In the study of computers, the number systems of importance are


binary, octal, and hexadecimal (their bases are powers of 2).

Digital Systems and Binary Numbers


NUMBER SYSTEMS
Decimal Binary Tertiary Quarternary Quinary Octal Hexadecimal
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 10 2 2 2 2 2
3 11 10 3 3 3 3
4 100 11 10 4 4 4
5 101 12 11 10 5 5
6 110 20 12 11 6 6
Examples: 7 111 21 13 12 7 7
8 1000 22 20 13 10 8
9 1001 100 21 14 11 9
10 1010 101 22 20 12 A
11 1011 102 23 21 13 B
12 1100 110 30 22 14 C
13 1101 111 31 23 15 D
14 1110 112 32 24 16 E
15 1111 120 33 30 17 F
16 1000 121 100 31 20 10

Digital Systems and Binary Numbers


NUMBER SYSTEMS

Digital Systems and Binary Numbers


NUMBER SYSTEMS

• To distinguish what number system is being used, the base


is usually written as a subscript to the number.

Examples:

1234510 (Base
(Base10)
10)
Hexadecimal numbers
may also be written
423415 (Base 5)
(Base 5)
as:
‒ 2C4ABH
1010102 (Base 2)
(Base 2)
‒ 0x2C4AB

2C4AB16 (Base 16)


(Base 16)

Digital Systems and Binary Numbers


COVERTING NON-BASE 10 NUMBERS TO BASE 10

• To convert a non-base 10 number to its base 10 or


decimal equivalent, the positional notation may be
used (with the appropriate base).

Examples:
3 2 1 0

42135 == 44 ×× 5533 + +2 2× ×525+2 1 +× 151×+531 ×+530 × 50


= 44 ××125
125 ++ 22××25 25 ++ 1 × 55 ++33 ×× 1
= 500 + 50 + 5 + 3
= 55810

Digital Systems and Binary Numbers


COVERTING NON-BASE 10 NUMBERS TO BASE 10
4 3 2 1 0
101102 = 11 ××2244 + 00 ××2233 + 1 ×× 2222 + 11 ××221 1 + 00 ××2200
= 1 × 16 + 0 × 8 + 1 × 4 + 1 × 2 + 0 × 1
= 16 + 0 + 4 + 2 + 0 = 2210
2 1 0 -1
374.28 = 33 ××8822 ++ 77××881 1 + 4 ×× 8800 ++ 22××88-1-1
= 3 × 64 + 7 × 8 + 4 × 1 + 2 × 0.125
= 192 + 56 + 4 + 0.25 = 252.2510
3 2 1 0
9AB216 = 99 ××16163 3 + AA××16
162 2 ++ BB××16
161 1 + 22 ××16
160 0
= 99 ××4096
4096 + 1010××256
256 + 11 11××16
16 + 22××11
= 36864 + 2560 + 176 + 2
= 3960210

Digital Systems and Binary Numbers


COVERTING NON-BASE 10 NUMBERS TO BASE 10

• Exercises

Convert the following numbers to their decimal equivalent:

34215

101010.012

D4B16

734.268

Digital Systems and Binary Numbers


BINARY NUMBER SYSTEM

• Since computers can only understand 0s and 1s, the binary number system is
the commonly used number system in the field of computing. So numbers are
expressed in terms of powers of two:

n 2n n 2n
0 1 11 2,048
1 2 12 4,096
2 4 13 8,192
3 8 14 16,384
4 16 15 32,768
5 32 16 65,536
6 64 17 131,072
7 128 18 262,144
8 256 19 524,288
9 512 20 1,048,576
10 1,024 21 2,097,152

Digital Systems and Binary Numbers


BINARY NUMBER SYSTEM

• A binary digit is called a bit. So


the binary number in the
bit example has 8 bits.

• The rightmost bit is called the


least significant bit (LSB).
100010012
• The leftmost bit is called the
most significant bit (MSB).
MSB LSB
• An 8-bit binary number is called a
byte. A 4-bit binary number is
called a nibble.

Digital Systems and Binary Numbers


CONVERTING BINARY NUMBERS TO DECIMAL

• As discussed earlier, to convert a binary number to


its decimal equivalent, the positional notation may
be used (with 2 as the base).

Example:
4 3 2 1 0
110112= 1 × 244 ++11××2323+ 0+ ×0 2×2 2+2 1 ×+ 211×+211 × +201 × 20
= 1 × 16 + 1 × 8 + 0 × 4 + 1 × 2 + 1 × 1
= 16 + 8 + 0 + 2 + 1
= 2710

Digital Systems and Binary Numbers


CONVERTING BINARY NUMBERS TO DECIMAL

• To make the conversion faster, use


the binary number equivalent of
each position.

Example:
128 64 32 16 8 4 2 1

1 1 0 1 0 1 1 02 = 128 + 64 + 16 + 4 + 2
= 21410
Digital Systems and Binary Numbers
CONVERTING BINARY NUMBERS TO DECIMAL

• Exercises

Convert the following binary numbers to decimal:

10112

1011102

011111012

1101.112

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO BINARY

• The conversion of a decimal integer to binary is done by dividing


the number and all successive quotients by 2 and accumulating
the remainders. This is repeated until the quotient becomes 0.

Example: Convert 5310 to binary.

53/2 = 26 remainder 1 LSB


26/2 = 13 remainder 0
13/2 = 6 remainder 1 5310 = 1101012
6/2 = 3 remainder 0
3/2 = 1 remainder 11
1/2 = 0 remainder 11 MSB

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO BINARY

• The conversion of a decimal fraction to binary is


accomplished by a method similar to that used for
integers, except that multiplication by 2 is used instead of
division, and integers are accumulated instead of
remainders. This is repeated until the remainder becomes
0.

Example: Convert 0.687510 to binary.

0.6875 × 2 = 1.375 integer = 1 MSB


0.375 × 2 ==0.75
0.75 integer = 0
integer = 1 0.687510 = 0.10112
0.75 ×× 2 ==1.5
1.5
0.5 ×× 22 ==1.0
1.0 integer = 1 LSB

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO BINARY

• There is the possibility that the repeated multiplication may not result in a
number with 0 remainder.

Example: Convert 0.6510 to binary.

0.65 ×× 22 = 1.3 integer = 1


0.3 × 2 = 0.6 integer = 0
0.6 × 2 = 1.2 integer = 1
0.2 × 2 = 0.4 integer = 0 This part will keep on
0.4 × 2 = 0.8 integer = 0 repeating indefinitely
0.8 × 2 = 1.6 integer = 1
0.6 × 2 = 1.2 integer = 1
0.2 × 2 = 0.4 integer = 0
0.4 × 2 = 0.8 integer = 0
0.8 × 2 = 1.6 integer = 1

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO BINARY

• In cases like this, it must be decided how many digits of


the fraction to use from the conversion and round or
truncate to this value.

Example: Convert 0.6510 to a 5-bit binary fraction.

0.65 ×× 22 ==1.3
1.3 integer
integer ==11 MSB
0.3 ×× 22 ==0.6
0.6 integer==00
integer
0.6 ×× 22 ==1.2
1.2 integer==11
integer 0.6510 = 0.101012
0.2 ×× 22 ==0.4
0.4 integer
integer ==00
0.4 ×× 22 ==0.8
0.8 integer
integer ==00
0.8 ×× 22 ==1.6
1.6 integer==11 LSB
integer

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO BINARY

• If the number has both integer and fractional parts, then


there is a need to separate the number into an integer
part and a fraction part, since the two parts must be
converted differently.

Example: Convert 13.37510 to binary.

13/2 == 66 rr 11
0.375 ×× 22 ==0.75
0.75 int
int ==00
6/2 == 3 r 00
0.75 × 22 ==1.51.5 int
int ==11
3/2 = 1 r 11
1/2 == 00 rr
0.5 × 2 ==1.0 1.0 int
int ==11
11

13.37510 == 1101
1101.011
.01122

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO BINARY

• Exercises

Convert the following decimal numbers to binary:

11110

25510

6410

386.812510

Digital Systems and Binary Numbers


CASE STUDY: UNITS OF MEASURE FOR DIGITAL INFORMATION

• SI Prefixes (International System of Units)

Kilo = 1,000 = 1033

Example: 5 KG = 5  1,000 = 5,000 grams

1,000  1,000
Mega = 1,000,000 = 106 == 1,000 1,000

Example: 8 MV = 8  1,000,000 == 8,000,000


8,000,000 volts
volts

1,000  1,000
Giga = 1,000,000,000 = 109 == 1,000 1,000  1,000
1,000

Example: 9 GW = 9  1,000,000,000 = 9,000,000,000 watts

Digital Systems and Binary Numbers


CASE STUDY: UNITS OF MEASURE FOR DIGITAL INFORMATION

• SI Prefixes as applied to digital information (bytes)

Kilo = 1,000 2103


1,024 = 10

Example: 4 KB = 4  1,000
1,024 = 5,000
4,096 grams
bytes

Mega = 1,000,000
1,048,576 = 10 1,024  1,000
2206 == 1,000 1,024

Example: 8 MB = 8  1,048,000
1,048,576 == 8,000,000
8,388,608volts
bytes

Giga = 1,000,000,000
1,073,741,824 = 10 1,024  1,000
2309 == 1,000 1,024  1,000
1,024


Example: 16 GB ==916 1,073,741,824
1,000,000,000 = 9,000,000,000 watts
= 17,179,869,184 bytes

Digital Systems and Binary Numbers


OCTAL AND HEXADECIMAL NUMBER SYSTEMS

• Aside from binary, the


number systems that are of
interest in the study of
computer architecture are
the octal (base 8) and
hexadecimal (base 16)
number systems since they
are basically systems based
on powers of two.

• They also provide a


convenient way of shortening
long binary numbers.

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO OCTAL

• The procedure for the conversion of a decimal


integer to octal is similar to that of binary except
that the number is divided by 8 instead of 2.

Example: Convert 151810 to octal.

1518/8== 189
189 remainder
remainder 66 LSD
189/8 = 2323 remainder
remainder 55
151810 = 27568
23/8 = 22 remainder
remainder 77
2/8 = 00 remainder
remainder 22 MSD

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO OCTAL

• The conversion of a decimal fraction to octal is also similar


to that of binary except that multiplication by 8 is used
instead of 2. Like in the binary conversion, the process of
multiplying fractions by 8 does not necessarily end with
zero, so it must be decided how many digits of the fraction
to use from the conversion.

Example: Convert 0.51310 to a 3-digit octal fraction.

0.513 × 8 ==4.104
4.104 integer
integer ==44 MSD
0.104 ×× 8 ==0.832
0.832 integer
integer ==00
0.51310 = 0.4078
0.832 ×× 8 ==6.656
6.656 integer = 6
integer = 6
0.656 ×× 8 ==5.248
5.248 integer ==55 LSD
integer

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO OCTAL

• Exercises

Convert the following decimal numbers to octal:

8310

304110

826.70117187510

Digital Systems and Binary Numbers


CONVERTING DECIMAL NUMBERS TO HEXADECIMAL

• The procedure for the conversion of a decimal


integer to hexadecimal is similar to that of binary
except that the number is divided by 16.

Example: Convert 478910 to hexadecimal.

4789/16 = 299
299 remainder
remainder 5 LSD
299/16 = 1818 remainder
remainder 11
11 478910 = 12B516
18/16 = 11 remainder
remainder 2
1/16 = 00 remainder
remainder 1 MSD
B
Digital Systems and Binary Numbers
CONVERTING DECIMAL NUMBERS TO HEXADECIMAL

• Exercises

Convert the following decimal numbers to


hexadecimal:

29110

1498010

2903.67187510

Digital Systems and Binary Numbers


CONVERTING OCTAL NUMBERS TO BINARY

• To convert octal to binary, simply convert each


octal digit into a 3-bit binary number.

Example: Convert 47238 to binary.

4 7 2 3

100 111 010 011

47238 = 1001110100112

Digital Systems and Binary Numbers


CONVERTING BINARY NUMBERS TO OCTAL

• To convert binary to octal, divide the binary


number into groups of 3 bits starting from the LSB
going to the MSB (from right to left). Then convert
each group into an octal number.

Example: Convert 10101101112 to octal.

1 010 110 111


1 2 6 7

10101101112 = 12678

Digital Systems and Binary Numbers


CONVERTING HEXADECIMAL NUMBERS TO BINARY

• To convert hexadecimal to binary, simply convert


each hexadecimal digit into a 4-bit binary number.

Example: Convert 4AE916 to binary.

4 A E 9

0100 1010 1110 1001

4AE916 = 01001010111010012

Digital Systems and Binary Numbers


CONVERTING BINARY NUMBERS TO HEXADECIMAL

• To convert binary to hexadecimal, divide the binary


number into groups of 4 bits starting from the LSB
going to the MSB. Then convert each group into a
hexadecimal number.

Example: Convert 10101101112 to hexadecimal.

10 1011 0111
2 B 7

10101101112 = 2B716

Digital Systems and Binary Numbers


CONVERTING OCTAL NUMBERS TO HEXADECIMAL

• To convert octal to hexadecimal, convert the


octal number to binary and then convert the
binary number to hexadecimal using the
procedures outlined earlier.

• Similarly, to convert hexadecimal to octal,


convert the hexadecimal number to binary
and then convert the binary number to octal
using the procedures outlined earlier.

Digital Systems and Binary Numbers


OCTAL AND HEXADECIMAL NUMBER SYSTEMS

• Example: Convert 47238 to hexadecimal.

4 7 2 3 1001 1101 0011


100 111 010 011 9 D 3

100111010011 9D3
47238 = 9D316
Digital Systems and Binary Numbers
OCTAL AND HEXADECIMAL NUMBER SYSTEMS

• Exercises: Complete the following table:

110101112 ______8 ______16

67458 ______2 ______16

F94C16 ______2 ______8

Digital Systems and Binary Numbers


UNSIGNED BINARY CODES

• So far, the binary numbers discussed represented unsigned integer numbers


(implied sign is positive).

• And more often than not, binary numbers are written using a fixed number of
bits.

Examples:

Represent 2610 in unsigned binary code.


2610 = 110102

Represent 2610 in unsigned binary code using 8 bits.


2610 = 00011010
000 2

Represent 2610 in unsigned binary code using 4 bits.


There are not enough bits to represent 2610.

Digital Systems and Binary Numbers


UNSIGNED BINARY CODES

• The decimal numbers that can be represented by 4 bits


are:
Binary Decimal Binary Decimal
0000 0 1000 8
0001 1 1001 9
0010 2 1010 10
0011 3 1011 11
0100 4 1100 12
0101 5 1101 13
0110 6 1110 14
0111 7 1111 15

Digital Systems and Binary Numbers


UNSIGNED BINARY CODES
• The decimal numbers that can be represented by 5 bits
are:
Binary Decimal Binary Decimal Binary Decimal Binary Decimal

00000 0 01000 8 10000 16 11000 24

00001 1 01001 9 10001 17 11001 25

00010 2 01010 10 10010 18 11010 26

00011 3 01011 11 10011 19 11011 27

00100 4 01100 12 10100 20 11100 28

00101 5 01101 13 10101 21 11101 29

00110 6 01110 14 10110 22 11110 30

00111 7 01111 15 10111 23 11111 31

Digital Systems and Binary Numbers


UNSIGNED BINARY CODES
• The range of values that can be represented with n bits
using unsigned binary code is given by the following
equation:

Range: 0 to 2n – 1

Examples:

to2244––1.1. (0
1. If n = 4 bits, then the range is 0 to (0to
to15)
15)

to 2255––1.1. (0
2. If n = 5 bits, then the range is 0 to (0 to
to31)
31)

to 2288––1.1. (0
3. If n = 8 bits, then the range is 0 to (0 to
to255)
255)

Digital Systems and Binary Numbers


UNSIGNED BINARY CODES

• Case Study: unsigned int in C++:

In C++, an unsigned int variable represents a number between 0 to


4,294,967,295.

This is because modern computer systems use 4 bytes (32 bits) to represent such
a value.

With 32 bits, the range of numbers that can be represented by an unsigned int
variable will be:

Range: 0 to 2n – 1

0 to 232 – 1

0 to 4,294,967,295

Digital Systems and Binary Numbers


UNSIGNED BINARY CODES

• Given a certain decimal number N, the number of bits, n,


needed to represent N is given by the following equation:

log 𝑁
𝑛=
log 2

Example:

If N = 26, the number of bits needed to represent N is:

log 26
𝑛= == 4.7 bits  55 bits
4.7bits bits
log 2

Digital Systems and Binary Numbers


SIGNED BINARY CODES

• There is also a need to represent signed decimal


numbers like +3210 or – 0.95310.

• In computations involving decimal numbers, a


minus sign is simply attached to the front of a
number to make the number negative.

• Since computers can only work with 0s and 1s


(they cannot understand the plus and minus sign),
a different approach is needed to represent both
positive and negative numbers.

Digital Systems and Binary Numbers


SIGNED BINARY CODES

• One solution is to add an extra bit to the front of our binary


number to indicate whether the number is positive or
negative.

• In computer terminology, this bit is called a sign bit.

• When the binary number is positive, the sign bit will be 0.


And when it is negative, the sign bit will be 1.

• This is called the Signed Magnitude Representation


approach. Notice that this is very similar to adding a plus or
minus sign in the decimal system.

Digital Systems and Binary Numbers


SIGNED BINARY CODES

• There are three ways in implementing


signed magnitude representation. They
are:

A. Sign and Magnitude Code

B. 1's Complement Code

C. 2's Complement Code


Digital Systems and Binary Numbers
SIGNED BINARY CODES (SIGN AND MAGNITUDE)

• Sign and Magnitude

In the sign and magnitude method, the MSB is used as the


sign bit.

If the sign bit is 0, this means the number is positive. If the


sign bit is 1, then the number is negative.

The remaining bits are used to represent the magnitude of


the binary number in the usual unsigned binary number
format.

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)
• Examples:

00110110 = + 5410
+
54 In unsigned numbers,
00110110 would be 5410.

10010110 == –– 22
2210

22 In unsigned numbers,
10010110 would be 15010.

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

To convert a positive decimal number to binary using sign


and magnitude:

1. Convert the number to its unsigned binary


equivalent.
2. Add a 0 at the leftmost bit position (sign bit).

To convert a negative decimal number to binary using sign


and magnitude :

1. Convert the number to its unsigned binary


equivalent.
2. Add a 1 at the leftmost bit position (sign bit).

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

• Examples:
Convert +2710 to a binary number using the sign and
magnitude representation:

2710 = 11011
110112 2 (unsigned)

+2710 = 011011
0 2 (add a 0 to its leftmost bit position)

Convert –2710 to a binary number using the sign and


magnitude representation:

2710 = 11011
110112 2 (unsigned)

–2710 = 11110112 (add a 1 to its leftmost bit position)

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

• Examples:
Convert +2710 to a 7-bit binary number using the sign and
magnitude representation:

2710 = 11011
110112 2 (unsigned)

+2710 = 0011011
00 2 (add two 0s to make it 7 bits)

Convert –2710 to a binary number using the sign and


magnitude representation:

2710 = 110112 (unsigned)


= 00110112 (add two 0s to make it 7 bits)
–2710 = 0011011
1 2 (adjust the sign bit)

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

• Exercises

Convert the following decimal numbers to binary using the


sign and magnitude representation:

–11010

–25610

–6410

+18210

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

• Exercises

Convert the following decimal numbers to 8-bit binary


using the sign and magnitude representation:

–11010

–25610

–3510

+2710

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

To convert a binary number that uses the sign and


magnitude representation to decimal:

1. Use the sign bit to determine if the number is


positive or negative.

2. Treat the remaining bits as if they represent


an unsigned binary number and convert it
to its decimal equivalent using the procedure
previously outlined. The decimal number
obtained is the magnitude of the number.

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

Example: If 10111012 follows the sign


and magnitude representation, what is
its decimal equivalent?
Take note that if the
problem states that
10111012 is an unsigned
1011101 binary number, then it is
equal to 9310.

– 0111012 = 2910

10111012 = –2910
Digital Systems and Binary Numbers
SIGNED BINARY CODES (SIGN AND MAGNITUDE)

• Exercises

Convert the following binary numbers that use sign and


magnitude representation to decimal:

1110112

0101012

101010002

10101112

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

Convert –8710 to a 7-bit binary number using the


sign and magnitude representation:

8710 = 1010111
10101112 2 (unsigned)

The problem has no answer since the unsigned


binary equivalent of 8710 is already 7 bits.

Using the sign and magnitude representation


requires an additional bit for the sign bit. So the
answer requires at least 8 bits.

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)
• The decimal numbers that can be represented by 4 bits
using sign and magnitude are:
Binary Decimal Binary Decimal
0000 +0 1000 -0
0001 +1 1001 -1
0010 +2 1010 -2
0011 +3 1011 -3
0100 +4 1100 -4
0101 +5 1101 -5
0110 +6 1110 -6
0111 +7 1111 -7

Digital Systems and Binary Numbers


SIGNED BINARY CODES (SIGN AND MAGNITUDE)

• The range of values that can be represented with n bits using sign
and magnitude binary code is given by the following equation:

–(2n-1n-1– –1)1) to +(2


Range: –(2 +(2n-1 – 1)1)
n-1 –

Examples:

–(233––1)1)toto
1. If n = 4 bits, the range is –(2 +(2+(2 3 – 1). (–7
3 – 1). (–7toto+7)
+7)

–(266––1)1)toto
2. If n = 7 bits, the range is –(2 +(2+(2 6 – 1). (–63
6 – 1). (–64toto+63)
+64)

–(277––1)1)toto
3. If n = 8 bits, the range is –(2 +(2+(2 7 – 1). (–127
7 – 1). (–128toto+127)
+128)

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)

• 1's Complement

To convert a positive decimal number to binary using 1's


complement:

1. Convert the number to its unsigned binary equivalent.


2. Add a 0 at the leftmost bit position (sign bit).

To convert a negative decimal number to binary using 1's


complement:

1. Convert the number to its unsigned binary equivalent.


2. Add a 0 at the leftmost bit position.
3. Complement each bit (change all 0s to 1s and vice versa).

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)

• Examples:
Convert +2710 to a binary number using the 1's complement
representation:

2710 = 11011
110112 2 (unsigned)
+2710 = 011011
0 2 (add a 0 to its leftmost position)

Convert –2710 to a binary number using the 1's complement


representation:

2710 = 110112 (unsigned)


= 0110112 (add a 0 to its leftmost position)

–2710 = 1100100
0 0 1 0 0 2 (complement each bit)

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)

• Examples:
Convert +2710 to a 7-bit binary number using the 1's
complement representation:

2710 = 11011
11011
2 2 (unsigned)
+2710 = 00
00110112 (add two 0s to its leftmost position)

Convert –2710 to a 7-bit binary number using the 1's


complement representation:

2710 = 110112 (unsigned)


= 00110112 (add two 0s to its leftmost position)

–2710 = 11100100
1 0 0 1 0 0 2 (complement each bit)

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)

• Exercises

Convert the following decimal numbers to binary using the


1's complement representation:

+6510

–3410

–13210

+2910

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)

• Exercises

Convert the following decimal numbers to 8-bit binary


using the 1's complement representation:

+6510

–3410

–13210

+2910

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)
To convert a binary number that uses the 1's complement
representation to decimal:

1. Use the sign bit to determine if the number is positive or


negative.

2. If positive, treat the binary number as if it is an unsigned


binary number and convert it to its decimal equivalent
using the procedure previously outlined. The decimal
number obtained is the magnitude of the number.

3. If negative, complement all the bits of the binary number.


Then treat the resulting binary number as if it is an
unsigned binary number and convert it to its decimal
equivalent using the procedure previously outlined. The
decimal number obtained is the magnitude of the number.

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)

Example: If 00111012 follows the 1's


complement representation, what is its
decimal equivalent?

0011101

+ 00111012 = 2910

00111012 = +2910
Digital Systems and Binary Numbers
SIGNED BINARY CODES (1'S COMPLEMENT)

Example: If 10111012 follows the 1's


complement representation, what is its
decimal equivalent?

1011101

– 01000102 = 3410
0100010

10111012 = –3410
Digital Systems and Binary Numbers
SIGNED BINARY CODES (1'S COMPLEMENT)

• Exercises

Convert the following binary numbers that use 1's


complement to decimal:

111111112

101010112

001010102

1111001112

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)

• The decimal numbers that can be represented by 4 bits


using 1's complement are:
Binary Decimal Binary Decimal
0000 +0 1000 -7
0001 +1 1001 -6
0010 +2 1010 -5
0011 +3 1011 -4
0100 +4 1100 -3
0101 +5 1101 -2
0110 +6 1110 -1
0111 +7 1111 -0

Digital Systems and Binary Numbers


SIGNED BINARY CODES (1'S COMPLEMENT)

• The range of values that can be represented with n bits using 1's
complement binary code is given by the following equation:

–(2n-1n-1– –1)1) to +(2


Range: –(2 +(2n-1 – 1)1)
n-1 –

Examples:

–(233––1)1)toto
1. If n = 4 bits, the range is –(2 +(2+(2 3 – 1). (–7
3 – 1). (–7toto+7)
+7)

–(266––1)1)toto
2. If n = 7 bits, the range is –(2 +(2+(2 6 – 1). (–63
6 – 1). (–64toto+63)
+64)

–(277––1)1)toto
3. If n = 8 bits, the range is –(2 +(2+(2 7 – 1). (–127
7 – 1). (–128toto+127)
+128)

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• 2's Complement

To convert a positive decimal number to binary using 2's


complement:
1. Convert the number to its unsigned binary equivalent.
2. Add a 0 at the leftmost bit position (sign bit).

To convert a negative decimal number to binary using 2's


complement:
1. Convert the number to its unsigned binary equivalent.
2. Add a 0 at the leftmost bit position.
3. Complement each bit (change all 0s to 1s and vice versa).
4. Add a 1 to the number.

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• Examples:
Convert +2710 to a binary number using the 2's complement representation:

2710 = 1101122 (unsigned)


+2710 = 011011
0 2 (add a 0 to its leftmost bit position)

Convert –2710 to a binary number using the 2's complement representation:

2710 = 110112 (unsigned)


= 0110112 (add a 0 to its leftmost bit position)

= 1001002 (complement each bit)


+ 1 (add a 1)

001012
–2710 = 1100101

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)
• Examples:
Convert +2710 to a 7-bit binary number using the 2's complement
representation:

2710 = 110112 (unsigned)


+2710 = 00110112 (add two 0s to its leftmost position)

Convert –2710 to a 7-bit binary number using the 2's complement


representation:

2710 = 110112 (unsigned)


= 00110112 (add two 0s to its leftmost position)

= 11001002 (complement each bit)


+ 1 (add a 1)

–2710 = 11100101
10 01012

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• Exercises

Convert the following decimal numbers to binary using the


2's complement representation:

+6510

–3410

–13210

+2910

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• Exercises

Convert the following decimal numbers to 8-bit binary


using the 2's complement representation:

+6510

–3410

–13210

+2910

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)
To convert a binary number that uses the 2's complement
representation to decimal:

1. Use the sign bit to determine if the number is positive or


negative.

2. If positive, treat the binary number as if it is an unsigned


binary number and convert it to its decimal equivalent
using the procedure previously outlined. The decimal
number obtained is the magnitude of the number.

3. If negative, complement all the bits of the binary number


and then add 1 to the result. Then treat the binary number
as if it is an unsigned binary number and convert it to its
decimal equivalent using the procedure previously outlined.
The decimal number obtained is the magnitude of the
number.
Digital Systems and Binary Numbers
SIGNED BINARY CODES (2'S COMPLEMENT)

Example: If 00111012 follows the 2's


complement representation, what is its
decimal equivalent?

0011101

+ 00111012 = 2910

00111012 = +2910
Digital Systems and Binary Numbers
SIGNED BINARY CODES (1'S COMPLEMENT)

Example: If 10111012 follows the 2's


complement representation, what is its
decimal equivalent?

1011101 01000102 = 3410


0100010
+ 1 = 2910
– 01000112 = 34
0100010 3510

10111012 = –3510
Digital Systems and Binary Numbers
SIGNED BINARY CODES (2'S COMPLEMENT)

• Exercises

Convert the following binary numbers that use 2's


complement to decimal:

111111112

101010112

001010102

111001112

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• The decimal numbers that can be represented by 4 bits


using 2's complement are:
Binary Decimal Binary Decimal
0000 +0 1000 -8
0001 +1 1001 -7
0010 +2 1010 -6
0011 +3 1011 -5
0100 +4 1100 -4
0101 +5 1101 -3
0110 +6 1110 -2
0111 +7 1111 -1

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• The range of values that can be represented with n bits using 2's
complement binary code is given by the following equation:

–(2n-1n-1) ) to
Range: –(2 +(2n-1n-1––1)1)
to +(2

Examples:

–(233) )toto+(2
1. If n = 4 bits, the range is –(2 3 –31).
+(2 – 1). (–8
(–8toto+7)
+7)

–(266))toto+(2
2. If n = 7 bits, the range is –(2 6 –61).
+(2 – 1). (–64
(–64 toto+63)
+63)

–(277))toto+(2
3. If n = 8 bits, the range is –(2 7 –71).
+(2 – 1). (–128
(–128 toto+127)
+127)

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• Case Study: int in C++:

In C++, an int variable (which is actually a signed int variable following 2’s
complement) represents a number between -2,147,483,648 to +2,147,483,647.

As mentioned before, modern computer systems use 4 bytes (32 bits) to


represent such a value.

With 32 bits, the range of numbers that can be represented by a signed int
variable will be:

Range: –(2n-1) to +(2n-1 – 1)

–(232-1) to +(232-1 – 1)

–2,147,483,648 to +2,147,483,647

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• Consider the following program:

#include <iostream>
using namespace std;

main() The output will be:


{
x = 2000000000
int x = 2000000000;
y = -1294967296
int y = 3000000000;

cout << "\n x = " << x;


cout << "\n y = " << y;
}

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• 3,000,000,000 in unsigned binary is:

1011 0010 1101 0000 0101 1110 0000 0000

• However, C++ treated the binary number as a 2's complement


number. Since the sign bit is 1, the number is negative. Its
magnitude is computed as:

0100 1101 0010 1111 1010 0001 1111 1111


+ 1

0100 1101 0010 1111 1010 0010 0000 0000 = 1,294,967,296

• So 3,000,000,000 was interpreted as - 1,294,967,296.

Digital Systems and Binary Numbers


SIGNED BINARY CODES (2'S COMPLEMENT)

• Exercises

Convert the following binary numbers that use 2's


complement to decimal:

111111112

101010112

001010102

111001112

Digital Systems and Binary Numbers


BINARY ARITHMETIC

• In its simplest form, a


computer consists of
five functionally Arithmetic
independent main parts: Input and
Logic

Memory
1. input,
Output Control
2. memory,
3. arithmetic and logic,
I/O Processor
4. output,
5. and control units.

Digital Systems and Binary Numbers


BINARY ARITHMETIC

• The part of the CPU Arithmetic


where arithmetic and Input and

logic operations are Logic

performed is called Memory


the Arithmetic and Output Control
Logic Unit (ALU). unit.
I/O Processor

Digital Systems and Binary Numbers


BINARY ARITHMETIC

• Adding two one-bit binary numbers:

0 1 0 1
+ 0 + 0 + 1 + 1
0 1 1 10
0

Carry-out

Digital Systems and Binary Numbers


BINARY ARITHMETIC

• Example: Add +810 and +710 using


5-bit binary numbers and 2's
complement representation:

+810 = 010002
+ +710 = 001112
011112 = 1510
01111
Digital Systems and Binary Numbers
BINARY ARITHMETIC

• Example: Add –1310 and +1010


using 5-bit binary numbers and 2's
complement representation:
1
–1310 = 100112
+ +1010 = 010102
111 012 = –310
11101
Digital Systems and Binary Numbers
BINARY ARITHMETIC

• Example: Subtract +610 from +1010 using 5-bit


binary numbers and 2's complement
representation. Take note that subtraction is
actually the addition of negative numbers. So the
subtraction 10 – 6 is actually 10 + (–6):
1 1 1

+1010 = 010102
+ – 610 = 110102
1100100
0 0100 2
The carry out
of the sign
bit is ignored 001002 = +410

Digital Systems and Binary Numbers


BINARY ARITHMETIC

• Exercises

Perform the following arithmetic operations using 8-bit


binary and two's complement:

7510 + 2810

–7010 – 5010

3510 – 5410

–4810 + 5110

Digital Systems and Binary Numbers


OVERFLOW

• Consider the following example: Add


+810 and +910 using 5-bit binary
numbers and 2's complement
representation:
1

+810 = 010002
+ +910 = 010012
10 0012 = –1510
10001
Digital Systems and Binary Numbers
OVERFLOW

• The reason for the error is that the answer, which


is supposed to be +1710, is out of range.
• For a 5-bit binary system using 2's complement,
the range of decimal numbers that can be
represented is from -1610 to +1510.

• When the result of an arithmetic operation falls


outside the representation limits, an overflow has
occurred and the arithmetic operation is
erroneous.

Digital Systems and Binary Numbers


OVERFLOW

• The chances of an overflow is high if the


operation involves the addition of two
positive numbers or two negative numbers.

• Specifically, an overflow occurs if:

1. There was a carry in to the sign bit but no


carry out of the sign bit, or
2. There was no carry in to the sign bit but
there was a carry out of the sign bit.
Digital Systems and Binary Numbers
OVERFLOW

• For example, if –70 is added to –75 using 8-bit


binary numbers and 2's complement
representation:
1 1 1

–7010 = 101110102
+ –7510 = 101101012
ignore 101101111
10110 11112 = +11110

Take note that there was an overflow because


there was no carry in to the sign bit but there was
a carry out of the sign bit.

Digital Systems and Binary Numbers


OVERFLOW

• Exercises

Perform the following arithmetic operations using 8-bit binary and


two's complement and determine if there is an overflow or not:

3410 – 1510

–7110 – 5710

3510 + 9910

–4810 + –2710

Digital Systems and Binary Numbers

You might also like