Digital Logic Design
Ali Saleh
Numbering Systems
Digital Computer
Systems
• Digital systems consider discrete amounts of data.
• Examples
– 26 letters in the alphabet
– 10 decimal digits
• Larger quantities can be built from discrete values:
– Words made of letters
– Numbers made of decimal digits (e.g. 239875.32)
• Computers operate on binary values (0 and 1)
• Easy to represent binary values electrically
– Voltages and currents.
– Can be implemented using circuits
– Create the building blocks of modern computers
Digital Logic Design - Ali Saleh 2
Understanding Decimal Numbers
• Decimal numbers are made of decimal digits: (0,1,2,3,4,5,6,7,8,9)
• Decimal number:
– 8653 = 8x103 + 6x102 + 5x101 + 3x100
• What about fractions?
– 97654.35 = 9x104 + 7x103 + 6x102 + 5x101 + 4x100 + 3x10-1 + 5x10-2
– In formal notation -> (97654.35)10
• Why do we use 10 digits, anyway?
Digital Logic Design - Ali Saleh 3
Understanding Binary Numbers
• Binary numbers are made of binary digits (bits):
– 0 and 1
• binary number:
– (1011)2 = 1x23 + 0x22 + 1x21 + 1x20 = (11)10
• What about fractions?
– (110.10)2 = 1x22 + 1x21 + 0x20 + 1x2-1 + 0x2-2
• Groups of eight bits are called a byte
– (11001001) 2
• Groups of four bits are called a nibble.
– (1101) 2
Digital Logic Design - Ali Saleh 4
Conversion Between Number Bases
Decimal(base 10) Binary(base 2)
Hexadecimal
(base16)
° Learn to convert between bases.
Digital Logic Design - Ali Saleh 5
Decimal review
• Numbers consist of a bunch of digits, each with a weight
1 6 2 . 3 7 5 Digits
100 10 1 1/10 1/100 1/1000 Weights
• These weights are all powers of the base, which is 10. We can rewrite
this:
1 6 2 . 3 7 5 Digits
102 101 100 10-1 10-2 10-3 Weights
• To find the decimal value of a number, multiply each digit by its weight
and sum the products.
(1 x 102) + (6 x 101) + (2 x 100) + (3 x 10-1) + (7 x 10-2) + (5 x 10-3) = 162.375
Digital Logic Design - Ali Saleh 6
Converting binary to decimal
• We can use the same trick to convert binary, or base 2, numbers to
decimal. This time, the weights are powers of 2.
– Example: 1101.01 in binary
1 1 0 1 . 0 1 Binary digits, or bits
23 22 21 20 2-1 2-2 Weights (in base 10)
– The decimal value is:
(1 x 23) + (1 x 22) + (0 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) =
8 + 4 + 0 + 1 + 0 + 0.25 = 13.25
Powers of 2: Useful abbreviations:
20 = 1 24 = 16 28 = 256 K = 210 = 1,024
21 = 2 25 = 32 29 = 512 M = 220 = 1,048,576
22 = 4 26 = 64 210 = 1024 G = 230 = 1,073,741,824
23 = 8 27 = 128
Digital Logic Design - Ali Saleh 7
Converting decimal to binary
• To convert a decimal integer into binary, keep dividing by 2 until the quotient is 0.
Collect the remainders in reverse order.
• To convert a fraction, keep multiplying the fractional part by 2 until it becomes 0.
Collect the integer parts in forward order.
• Example: 162.375:
162 / 2 = 81 rem 0 0.375 x 2 = 0.750
81 / 2 = 40 rem 1 0.750 x 2 = 1.500
40 / 2 = 20 rem 0 0.500 x 2 = 1.000
20 / 2 = 10 rem 0
10 / 2 =5 rem 0
5/2 =2 rem 1
2/2 =1 rem 0
1/2 =0 rem 1
• So, 162.37510 = 10100010.0112
Digital Logic Design - Ali Saleh 8
Base 16 is useful too
• The hexadecimal system uses 16 digits:
Decimal Binary Hex
Decimal Binary Hex
0123456789ABCDEF 00 0000
0000 00
11 0001 11
• You can convert between base 10 and base
22
0001
0010 22
16 using techniques like the ones we just 0010
33 0011
0011 33
showed for converting between decimal and
44 0100
0100 44
binary.
55 0101
0101 55
• For our purposes, base 16 is most useful as 66 0110 66
0110
a “shorthand” notation for binary numbers. 77 0111 77
0111
– Since 16 = 24, one hexadecimal digit is 88 1000
1000 88
equivalent to 4 binary digits. 99 1001
1001 99
– It’s often easier to work with a number 10
10 1010
1010 AA
like B4 instead of 10110100. 1111 1011
1011 BB
12
12 1100
1100 CC
13
13 1101
1101 DD
14
14 1110
1110 EE
15
15 1111
1111 FF
Digital Logic Design - Ali Saleh 9
Binary and hexadecimal conversions
• Converting from hexadecimal to binary is easy: just replace each hex
digit with its equivalent 4-bit binary sequence.
261.3516 = 2 6 1 . 3 516
= 0010 0110 0001 . 0011 01012
• To convert from binary to hex, make groups of 4 bits, starting from
the binary point. Add 0s to the ends of the number if needed. Then,
just convert each bit group to its corresponding hex digit.
10110100.0010112 = 1011 0100 . 0010 11002
= B 4 . 2 C16
Hex Binary Hex Binary Hex Binary Hex Binary
0 0000 4 0100 8 1000 C 1100
1 0001 5 0101 9 1001 D 1101
2 0010 6 0110 A 1010 E 1110
3 0011 7 0111 B 1011 F 1111
Digital Logic Design - Ali Saleh 10
Hex to Decimal
8 7 C 9
x 16
128
+ 7
135
x 16
2,160
+ 12
2,172
x 16
34,752
+ 9
34,761
Digital Logic Design - Ali Saleh 11
Convert Decimal to Hex
Integer Part: Divide by the base,
keep track of the remainder, and read up.
16 34,761
16 2,172 rem 9
16 135 rem 12 = C Read up
16 8 rem 7
0 rem 8
34,76110 = 87C916
Digital Logic Design - Ali Saleh 12
Questions
100101012 = ? (decimal)
149
Digital Logic Design - Ali Saleh 13
Questions
85710 = ?16
559
Digital Logic Design - Ali Saleh 14
Questions
BED16 = ?2
1011 1110 1101
Digital Logic Design - Ali Saleh 15
Addition
Digital Logic Design - Ali Saleh 16
Binary
Addition
• Binary addition is very simple.
• This is best shown in an example of adding two binary numbers…
1 1 1 1 1
1 carries
1
1 1 1 0 1
+ 1 0 1 1 1
---------------------
1 0 1 0 1 0 0
Digital Logic Design - Ali Saleh 17
Binary addition example worked out
• Some terms are given here
• Exercise: what are these numbers equivalent to in decimal?
The initial carry
in is implicitly 0
1 1 1 0 (Carries)
1 0 1 1 (Augend)
+ 1 1 1 0 (Addend)
1 1 0 0 1 (Sum)
most significant least significant
bit (MSB) bit (LSB)
Digital Logic Design - Ali Saleh 18
Subtraction
Digital Logic Design - Ali Saleh 19
Binary
Subtraction
° We can also perform subtraction (with borrows in place of
carries).
° Let’s subtract (10111)2 from (1001101)2…
1 10 borrows
0 10 10 0 0 10
1 0
0 1 1 0 1
- 1 0 1 1 1
------------------------
1 1 0 1 1 0
Digital Logic Design - Ali Saleh 20
Negative Numbers
Subtract by adding
73 73
-35 10’s complement +65
38 138
Ignore carry
Digital Logic Design - Ali Saleh 21
Two’s Complement Shortcuts
• Algorithm – Simply complement each bit and then add 1 to the
result.
– Finding the 2’s complement of (01100101)2 and of its 2’s
complement…
N = 01100101 [N] = 10011011
10011010 01100100
+ 1 + 1
--------------- ---------------
10011011 01100101
Digital Logic Design - Ali Saleh 22
Negative Numbers
Subtract by adding
73 01001001 73 01001001
-35 1 10111 01 35 00100011
38 00100110 2’s comp 11011100 flip
+1
-----------
-35 11011101
Digital Logic Design - Ali Saleh 23
Table 2.2
Positive and Negative Binary Numbers
Signed decimal Hex Binary Unsigned decimal
-128 80 10000000 128
-127 81 10000001 129
-126 82 10000010 130
… … … …
… … … …
… … … …
-3 FD 11111101 253
-2 FE 11111110 254
-1 FF 11111111 255
0 00 00000000 0
1 01 00000001 1
2 02 00000010 2
3 03 00000011 3
… … … …
… … …
… … …
125 7D 01111101 125
126 7E 01111110 126
127 7F 01111111 127
Digital Logic Design - Ali Saleh 24
Signed Numbers
4-bit: 8H = -8 to 7H = +7
1000 to 0111
8-bit: 80H = -128 to 7F = +127
16-bit: 8000H = -32,768 to
7FFFH = +32,767
32-bit: 80000000H = -2,147,483,648 to
7FFFFFFFH = +2,147,483,647
Digital Logic Design - Ali Saleh 25
Questions
What is the two’s complement of
00101100?
1101 0100
What hex number represents the
decimal number -40? Binary 11011000
Hex D8
Digital Logic Design - Ali Saleh 26
Multiplication
Digital Logic Design - Ali Saleh 27
Binary
Multiplication
• Binary multiplication is much the same as decimal multiplication,
except that the multiplication operations are much simpler…
1 0 1 1 1
X 1 0 1 0
-----------------------
0 0 0 0 0
1 0 1 1 1
0 0 0 0 0
1 0 1 1 1
-----------------------
1 1 1 0 0 1 1 0
Digital Logic Design - Ali Saleh 28
ASCII CODES
Digital Logic Design - Ali Saleh 29
ASCII
• The most commonly used code for representing letters, numerals
and punctuation characters (alphanumeric data)
• Each character is represented with a 7-bit string; for example:
‘3’ = 00110011 (hex 33)
‘ ’ = 00100000 (hex 20)
• An 8-bit extension of ASCII has also been defined
Digital Logic Design - Ali Saleh 30
ASCII
Code
• American Standard Code for Information Interchange
• ASCII is a 7-bit code, frequently used with an 8 th bit for error
detection (more about that in a bit).
Character ASCII (bin) ASCII (hex) Decimal
A 01000001 41 65
B 01000010 42 66
C 01000011 43 67
…
Z
a
…
1
‘
Digital Logic Design - Ali Saleh 31
ASCII Properties
Q1:
What is relationship between a decimal digit (0, 1, …) and
its ASCII code?
Digital Logic Design - Ali Saleh 32
ASCII Properties (2)
Q2:
What is the difference between an upper-case letter
(A, B, …) and its lower-case equivalent (a, b, …)? ASCI CODE
Digital Logic Design - Ali Saleh 33
BCD
Digital Logic Design - Ali Saleh 34
Binary Coded Decimal
Digit BCD Code Digit BCD Code
0 0000 5 0101
1 0001 6 0110
2 0010 7 0111
3 0011 8 1000
4 0100 9 1001
• Binary coded decimal (BCD) represents each decimal digit with four bits
– Ex. 0011 0010 1001 = 329BCD
3 2 9
• This is NOT the same as 0011001010012
• Why do this? Because people think in decimal.
Digital Logic Design - Ali Saleh 35
Putting It All
Together
° BCD not very efficient
° Used in early
computers (40s, 50s)
° Used to encode
numbers for seven-
segment displays.
° Easier to read?
Digital Logic Design - Ali Saleh 36