LEC1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

COMP

302 Digital Design and Computer Architecture

LEC Binary numbers and logic


1 circuits
Textbook chapter 1 & 2

Fall 2024 Dr. Amr Hefny Cairo University


TEXTBOOKS AND REFERENCE

Textbook:
❑ Sarah L. Harris, David Harris, “Digital Design and Computer
Architecture: ARM Edition 1st Edition”, ISBN-13: 978-0128000564

References:
❑ M. Morris Mano and Michael Ciletti, “Digital Design” Global Edition,
ISBN 978-1292231167

❑ Thomas L. Floyd, “Digital Fundamentals”, ISBN 978-0131946095

2
Abstraction
programs
Hiding details when they
aren’t important
device drivers

instructions
registers

focus of this course


datapaths
controllers

adders
memories

AND gates
NOT gates

amplifiers
filters

transistors
diodes

electrons

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <3>
Digital Discipline: Binary Values
• Two discrete values:
• 1’s and 0’s
• 1, TRUE, HIGH
• 0, FALSE, LOW
• 1 and 0: voltage levels, rotating gears, fluid levels,
etc.
• Digital circuits use voltage levels to represent 1
and 0
• Bit: Binary digit

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <4>
Number Systems
• Decimal numbers

1000's column

10's column
1's column
100's column

537410 = 5 × 103 + 3 × 102 + 7 × 101 + 4 × 100


five three seven four
thousands hundreds tens ones

• Binary numbers
8's column

2's column
1's column
4's column

11012 = 1 × 23 + 1 × 22 + 0 × 21 + 1 × 20 = 1310
one one no one
eight four two one

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <5>
Powers of Two
• 20 = • 28 =
• 21 = • 29 =
• 22 = • 210 =
• 23 = • 211 =
• 24 = • 212 =
• 25 = • 213 =
• 26 = • 214 =
• 27 = • 215 =

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <6>
Powers of Two
• 20 = 1 • 28 = 256
• 21 = 2 • 29 = 512
• 22 = 4 • 210 = 1024 Handy to
• 23 = 8 • 211 = 2048 memorize
• 24 = 16 • 212 = 4096 up to 29
• 25 = 32 • 213 = 8192
• 26 = 64 • 214 = 16384
• 27 = 128 • 215 = 32768

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <7>
Number Conversion
• Binary to decimal conversion:
– Convert 100112 to decimal

• Decimal to binary conversion:


– Convert 4710 to binary

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <8>
Number Conversion
• Binary to decimal conversion:
– Convert 100112 to decimal
– 16×1 + 8×0 + 4×0 + 2×1 + 1×1 = 1910

• Decimal to binary conversion:


– Convert 4710 to binary
– 32×1 + 16×0 + 8×1 + 4×1 + 2×1 + 1×1 = 1011112

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <9>
Decimal to Binary Conversion
• Two methods:
o Method 1: Find the largest power of 2 that fits,
subtract and repeat
o Method 2: Repeatedly divide by 2, remainder goes in
next most significant bit

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <10>
Decimal to Binary Conversion
Method 1: Find the largest power of 2 that fits, subtract and repeat
5310

Method 2: Repeatedly divide by 2, remainder goes in next most significant bit


5310

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <11>
Decimal to Binary Conversion
Method 1: Find the largest power of 2 that fits, subtract and repeat
5310 32×1
53-32 = 21 16×1
21-16 = 5 4×1
5-4 = 1 1×1
= 1101012
Method 2: Repeatedly divide by 2, remainder goes in next most significant bit
5310 = 53/2 = 26 R1
26/2 = 13 R0
13/2 = 6 R1
6/2 = 3 R0
3/2 = 1 R1
1/2 = 0 R1 = 1101012

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <12>
Decimal to Binary Conversion
Another example: Convert 7510 to binary.

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <13>
Decimal to Binary Conversion
Another example: Convert 7510 to binary.
7510= 64 + 8 + 2 + 1 = 10010112

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <14>
Decimal to Binary Conversion
Another example: Convert 7510 to binary.
7510= 64 + 8 + 2 + 1 = 10010112
or
75/2 = 37 R1
37/2 = 18 R1
18/2 = 9 R0
9/2 = 4 R1
4/2 = 2 R0
2/2 = 1 R0
1/2 = 0 R1

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <15>
Binary Values and Range
• N-digit decimal number
• How many values?
• Range?
• Example: 3-digit decimal number:

• N-bit binary number


• How many values?
• Range:
• Example: 3-digit binary number:

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <16>
Binary Values and Range
• N-digit decimal number
• How many values? 10N
• Range? [0, 10N - 1]
• Example: 3-digit decimal number:
• 103 = 1000 possible values
• Range: [0, 999]

• N-bit binary number


• How many values? 2N
• Range: [0, 2N - 1]
• Example: 3-digit binary number:
• 23 = 8 possible values
• Range: [0, 7] = [0002 to 1112]

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <17>
Hexadecimal Numbers
Hex Digit Decimal Equivalent Binary Equivalent
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
A 10
B 11
C 12
D 13
E 14
F 15

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <18>
Hexadecimal Numbers
Hex Digit Decimal Equivalent Binary Equivalent
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <19>
Hexadecimal Numbers
• Base 16
• Shorthand for binary

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <20>
Hexadecimal to Binary Conversion
• Hexadecimal to binary conversion:
– Convert 4AF16 (also written 0x4AF) to binary

• Hexadecimal to decimal conversion:


– Convert 0x4AF to decimal

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <21>
Hexadecimal to Binary Conversion
• Hexadecimal to binary conversion:
– Convert 4AF16 (also written 0x4AF) to binary
– 0100 1010 11112

• Hexadecimal to decimal conversion:


– Convert 4AF16 to decimal
– 162×4 + 161×10 + 160×15 = 119910

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <22>
Bits, Bytes, Nibbles…
• Bits 10010110
most least
significant significant
bit bit
byte
• Bytes & Nibbles
10010110
nibble

• Bytes CEBF9AD7
most least
significant significant
byte byte

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <23>
Large Powers of Two
• 210 = 1 kilo ≈ 1000 (1024)
• 220 = 1 mega ≈ 1 million (1,048,576)

• 230 = 1 giga ≈ 1 billion (1,073,741,824)

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <24>
Estimating Powers of Two
• What is the value of 224?

• How many values can a 32-bit variable


represent?

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <25>
Estimating Powers of Two
• What is the value of 224?
24 × 220 ≈ 16 million

• How many values can a 32-bit variable


represent?
22 × 230 ≈ 4 billion

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <26>
Addition
• Decimal
3734
+ 5168

• Binary
1011
+ 0011

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <27>
Addition
• Decimal 11 carries
3734
+ 5168
8902

• Binary 11 carries
1011
+ 0011
1110

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <28>
Binary Addition Examples
• Add the following 4-
bit binary numbers 1001
+ 0101

• Add the following 4-


bit binary numbers 1011
+ 0110

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <29>
Binary Addition Examples
• Add the following 4- 1
bit binary numbers 1001
+ 0101
1110

• Add the following 4- 111


bit binary numbers 1011
+ 0110
10001

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <30>
Binary Addition Examples
• Add the following 4- 1
bit binary numbers 1001
+ 0101
1110

• Add the following 4- 111


bit binary numbers 1011
+ 0110
10001
Overflow!
Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <31>
Overflow
• Digital systems operate on a fixed number of
bits
• Overflow: when result is too big to fit in the
available number of bits
• See previous example of 11 + 6

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <32>
Signed Binary Numbers
• Sign/Magnitude Numbers
• Two’s Complement Numbers

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <33>
Sign/Magnitude Numbers
• 1 sign bit, N-1 magnitude bits
• Sign bit is the most significant (left-most) bit
– Positive number: sign bit = 0
– Negative number: sign bit = 1

• Example, 4-bit sign/mag representations of ± 6:


+6 =
-6=

• Range of an N-bit sign/magnitude number:

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <34>
Sign/Magnitude Numbers
• 1 sign bit, N-1 magnitude bits
• Sign bit is the most significant (left-most) bit
– Positive number: sign bit = 0
– Negative number: sign bit = 1

• Example, 4-bit sign/mag representations of ± 6:


+6 = 0110
- 6 = 1110

• Range of an N-bit sign/magnitude number:


[-(2N-1-1), 2N-1-1]

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <35>
Sign/Magnitude Numbers
• 1 sign bit, N-1 magnitude bits
• Sign bit is the most significant (left-most) bit
– Positive number: sign bit = 0
– Negative number: sign bit = 1

• Example, 4-bit sign/mag representations of ± 6:


+6 = 0110
- 6 = 1110

• Range of an N-bit sign/magnitude number:


[-(2N-1-1), 2N-1-1]

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <36>
Sign/Magnitude Numbers
Problems:
• Addition doesn’t work, for example -6 + 6:
1110
+ 0110
10100 (wrong!)

• Two representations of 0 (± 0):


1000
0000
Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <37>
Two’s Complement Numbers
• Don’t have same problems as sign/magnitude
numbers:
– Addition works
– Single representation for 0

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <38>
Two’s Complement Numbers
• msb has value of -2N-1

• Most positive 4-bit number:


• Most negative 4-bit number:
• The most significant bit still indicates the sign (1
= negative, 0 = positive)
• Range of an N-bit two’s complement number:

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <39>
Two’s Complement Numbers
• msb has value of -2N-1

• Most positive 4-bit number: 0111


• Most negative 4-bit number: 1000
• The most significant bit still indicates the sign (1
= negative, 0 = positive)
• Range of an N-bit two’s complement number:
[-(2N-1), 2N-1-1]
Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <40>
“Taking the Two’s Complement”
• “Taking the Two’s complement” flips the
sign of a two’s complement number
• Method:
1. Invert the bits
2. Add 1
• Example: Flip the sign of 310 = 00112

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <41>
“Taking the Two’s Complement”
• “Taking the Two’s complement” flips the
sign of a two’s complement number
• Method:
1. Invert the bits
2. Add 1
• Example: Flip the sign of 310 = 00112
1. 1100
2. + 1
1101 = -310

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <42>
Two’s Complement Examples
• Take the two’s complement of 610 = 01102

• What is the decimal value of the two’s


complement number 10012?

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <43>
Two’s Complement Examples
• Take the two’s complement of 610 = 01102
1. 1001
2. + 1
10102 = -610

• What is the decimal value of the two’s


complement number 10012?
1. 0110
2. + 1
01112 = 710, so 10012 = -710

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <44>
Two’s Complement Addition
• Add 6 + (-6) using two’s complement numbers

0110
+ 1010

• Add -2 + 3 using two’s complement numbers

1110
+ 0011

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <45>
Two’s Complement Addition
• Add 6 + (-6) using two’s complement numbers
111
0110
+ 1010
10000
• Add -2 + 3 using two’s complement numbers
111
1110
+ 0011
10001
Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <46>
Increasing Bit Width
Extend number from N to M bits (M > N) :
– Sign-extension
– Zero-extension

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <47>
Copyright © 2012 Elsevier
Sign-Extension
• Sign bit copied to msb’s
• Number value is same
• Example 1:
– 4-bit representation of 3 = 0011
– 8-bit sign-extended value: 00000011
• Example 2:
– 4-bit representation of -5 = 1011
– 8-bit sign-extended value: 11111011

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <48>
Zero-Extension
• Zeros copied to msb’s
• Value changes for negative numbers
• Example 1:
– 4-bit value = 0011 = 310
– 8-bit zero-extended value: 00000011 = 310
• Example 2:
– 4-bit value = 1011 = -510
– 8-bit zero-extended value: 00001011 = 1110

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <49>
Number System Comparison
Number System Range
Unsigned [0, 2N-1]
Sign/Magnitude [-(2N-1-1), 2N-1-1]
Two’s Complement [-2N-1, 2N-1-1]

For example, 4-bit representation:


-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Unsigned 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 Two's Complement

0000
1111 1110 1101 1100 1011 1010 1001
1000
0001 0010 0011 0100 0101 0110 0111 Sign/Magnitude

Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <50>
Digital Design and Computer Architecture: ARM® Edition © 2015 Chapter 6 <51>

You might also like