<<

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW ECE-238L: Computer Logic Design Fall 2013

Notes - Chapter 2

BINARY NUMBER CONVERSIONS

DECIMAL NUMBER SYSTEM DIGIT

. A digit can take values from 0 to 9: 0 1 2 3 4 5 6 7 8 9 . Digit-by-digit representation of a positive integer number (powers of 10):

Number: 9372 hundreds tens

thousands units

9 3 7 2

9 thousands, 3 hundreds, 7 tens, and 2 units

103 102 101 100

9372 = 9×103 + 3×102 + 7×101 + 2×100

Positional Number Representation . Let’s consider the numbers from 0 to 999. We represent these numbers with 3 digits (each digit being a number between 0 and 9). We show a 3-digit number using the positional number representation:

MATHEMATICAL REPRESENTATION EXAMPLE 3-digit

d2 d1 d0 2 0 9

Third Digit Second Digit First Digit Third Digit Second Digit First Digit

. The positional number representation allows us to express the decimal value using powers of ten: 2 1 0 퐷 = 푑2 × 10 + 푑1 × 10 + 푑0 × 10 . Example:

Decimal 3-digit representation Powers of 10: 2 1 0 Number d2d1d0 d2 10 + d1 10 + d0 10 2 1 0 0 000 0 10 + 0 10 + 0 10 9 009 0 102 + 0 101 + 9 100 11 011 0 102 + 1 101 + 1 100 25 025 0 102 + 2 101 + 5 100 90 090 0 102 + 9 101 + 0 100 2 1 0 128 128 1 10 + 2 10 + 8 10 2 1 0 255 255 2 10 + 5 10 + 5 10

Exercise: Write down the 3-digit and the powers of ten representations for the following numbers:

2 1 0 Decimal Number 3-digit representation 푑2 × 10 + 푑1 × 10 + 푑0 × 10

5

254

100

99

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

General Case: . Positional number representation for an integer positive number with ‘n’ digits: dn-1dn-2 ... d1d0

n-1 n-2 1 0 Decimal Value: dn-1 10 + dn-2 10 + ... + d1 10 + d0 10

. Example: 1098324 (7 digits). 1098324 1106  0 105  9 104  8 103  3102  2 101  4 100 203476 (6 digits). 203476  2 105  0 104  3103  4 102 7 101  6 100

Maximum value: . The table presents the maximum attainable value for a given number of digits. What pattern do you find? Can you complete it for the highlighted cases (4 and 6)?

Number of digits Maximum value Range 1 9 = 101-1 0  9  0  101-1 2 99 = 102-1 0  99  0  102-1 3 999 = 103-1 0  999  0  103-1 4 5 99999 = 105-1 0  99999  0  105-1 6 … n 999…999 = 10n-1 0  999…999  0  10n-1

. Maximum value for a number with ‘n’ digits: Based on the table, the maximum decimal value for a number with ‘n’ digits is given by: n-1 n-2 1 0 n D = 999...999 = 9 10 + 9 10 + ... + 9 10 + 9 10 = 10 -1 n digits

 With ‘n’ digits, we can represent positive integer numbers from 0 to 10n-1.

 With 7 digits, what is the range (starting from 0) of positive numbers that we can represent? How many different numbers can we represent?

BINARY NUMBER SYSTEM

. We are used to the decimal number system. However, DIGIT BIT there exist other number systems: , , vigesimal, binary, etc. In particular, binary numbers are 0 1 2 3 4 5 6 7 8 9 0 1 very practical as they are used by digital computers. For binary numbers, the counterpart of digit (that can take values from 0 to 9) is the bit (binary digit) that can take the value of 0 or 1.

. Bit: Unit of information that a computer uses to process and retrieve data. It can also be used as a logical variable (see Chapter 1).

. Binary representation: We use the positional number representation. Note that here, we are using a string of bits to represent a binary number.

. Converting a binary number into a decimal number: The following figure depicts two cases: 2-bit numbers and 3-bit numbers. Note that the positional representation with powers of two let us obtain the decimal value (integer positive) of the binary number.

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013 Binary number Powers of 2: Decimal 1 0 b1b0 b1 2 + b0 2 Number MATHEMATICAL REPRESENTATION 2-bit 1 0 b b 00 0 2 + 0 2 0 1 0 1 0 01 0 2 + 1 2 1 10 1 21 + 0 20 2 Second Bit First Bit 11 1 21 + 1 20 3

Binary number Powers of 2: Decimal 2 1 0 b2b1b0 b2 2 + b1 2 + b0 2 Number

2 1 0 MATHEMATICAL REPRESENTATION 000 0 2 + 0 2 + 0 2 0 2 1 0 3-bit 001 0 2 + 0 2 + 1 2 1 b b b 010 0 22 + 1 21 + 0 20 2 2 1 0 011 0 22 + 1 21 + 1 20 3 2 1 0 100 1 2 + 0 2 + 0 2 4 2 1 0 Third Bit Second Bit First Bit 101 1 2 + 0 2 + 1 2 5 110 1 22 + 1 21 + 0 20 6 111 1 22 + 1 21 + 1 20 7

General case: . Positional number representation for a binary number with ‘n’ bits: b b ... b b n-1 n-2 1 0 Most significant Least significant (leftmost) bit (rightmost) bit

This formula converts a binary number into a decimal number: Decimal Value (integer positive): n-1 n-2 1 0 D = bn-1 2 + bn-2 2 + ... + b1 2 + b0 2

. To avoid confusion, we usually write a binary number and attach a suffix ‘2’: (푏푛−1푏푛−2 … 푏1푏0)2

5 4 3 2 1 0 . Example: 6 푏푖푡푠: (101011)2 ≡ 퐷 = 1 × 2 + 0 × 2 + 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2 = 43 3 2 1 0 4 푏푖푡푠: (1011)2 ≡ 퐷 = 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2 = 11

. Maximum value for a given number of bits. Complete the tables for the highlighted cases (4 and 6):

Number of bits Maximum value Range 1 1 1 12  2 -1 0  12  0  2 -1

2 2 2 112  2 -1 0  112  0  2 -1

3 3 3 1112  2 -1 0  1112  0  2 -1 4

5 5 5 111112  2 -1 0  111112  0  2 -1 6 …

n n n 111…1112  2 -1 0  111…1112  0  2 -1

. Maximum value for ‘n’ bits: The maximum decimal value with ‘n’ bits is given by: n-1 n-2 1 0 n D = 111...111 = 1 2 + 1 2 + ... + 1 2 + 1 2 = 2 -1 n bits  With ‘n’ bits, we can represent positive integer numbers from 0 to 2n-1. (2n numbers)

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

. The case n=8 bits is of particular interest, as a string of 8 bits is called a byte. Here, the range for 8-bit numbers is 0 to 28-1  0 to 255. b b b b b b b b 7 6 5 4 3 2 1 0 Most significant Least significant (leftmost) bit (rightmost) bit

. The table shows some examples:

Decimal 8-bit format b 27 + b 26 + b 25 + b 24 + b 23 + b 22 + b 21 + b 20 Number b7b6b5b4b3b2d1d0 7 6 5 4 3 2 1 0

7 6 5 4 3 2 1 0 0 00000000 0 2 + 0 2 + 0 2 + 0 2 + 0 2 + 0 2 + 0 2 + 0 2 9 00001001 0 27 + 0 26 + 0 25 + 0 24 + 1 23 + 0 22 + 0 21 + 1 20 11 00001011 0 27 + 0 26 + 0 25 + 0 24 + 1 23 + 0 22 + 1 21 + 1 20 25 00011001 0 27 + 0 26 + 0 25 + 1 24 + 1 23 + 0 22 + 0 21 + 1 20 90 01011010 0 27 + 1 26 + 0 25 + 1 24 + 1 23 + 0 22 + 1 21 + 0 20 128 10000000 1 27 + 0 26 + 0 25 + 0 24 + 0 23 + 0 22 + 0 21 + 0 20 255 11111111 1 27 + 1 26 + 1 25 + 1 24 + 1 23 + 1 22 + 1 21 + 1 20

Exercise: Convert the following binary numbers (positive integers) to their decimal values: 8-bit Decimal 7 6 5 4 3 2 1 0 representation 푏7 × 2 + 푏6 × 2 + 푏5 × 2 + 푏4 × 2 + 푏3 × 2 + 푏2 × 2 + 푏1 × 2 + 푏0 × 2 Number

00000001

00001001

10000101

10000111

11110011

. In general to convert a number of base 'r' (r = 2, 3,4 ,…) to decimal, we use the following formula: Number in base 'r': (푟푛−1푟푛−2 … 푟1푟0)푟 푛−1 푛−2 1 0 Conversion to decimal: 퐷 = 푟푛−1 × 푟 + 푟푛−2 × 푟 + ⋯ + 푟1 × 푟 + 푟0 × 푟 Also, the maximum decimal value for a number in base 'r' with 'n' digits is: 푛−1 푛−2 1 0 푛 퐷 = 푟푟푟 … 푟푟푟 = 푟 × 푟 + 푟푛−2 × 푟 + ⋯ + 푟 × 푟 + 푟 × 푟 = 푟 − 1

Example: Base-8: Number of digits Maximum value Range 1 1 1 78  8 -1 0  78  0  8 -1

2 2 2 778  8 -1 0  778  0  8 -1

3 3 3 7778  8 -1 0  7778  0  8 -1 …

n n n 777…7778  8 -1 0  777…7778  0  8 -1

Examples: . (50632)8: Number in base 8 (octal system) Number of digits: n = 5 Conversion to decimal: 퐷 = 5 × 84 + 0 × 83 + 6 × 82 + 3 × 81 + 2 × 80 = 20890 . (3102)4: Number in base 4 (quaternary system) Number of digits: n = 4 Conversion to decimal: 퐷 = 3 × 43 + 1 × 42 + 0 × 41 + 2 × 40 = 210

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

CONVERSION OF DECIMAL TO BINARY NUMBERS . Two examples are shown:

Number in Number in Number in Number in base 10 base 2 base 10 base 2

52 ????2 123 ????2

Remainder Remainder 26 61 0 1 2 2 52 123

13 30 0 1 2 2 26 61

6 15 1 0 2 13 2 30

110100 3 2 7 0 1 11110112 2 6 2 15

1 3 1 1 2 3 2 7

0 1 1 1 2 1 2 3

stop here! 0 1 2 1 stop here!

. Note that some numbers require fewer bits than others. If we want to use a specific bit representation, e.g., 8-bit, we just need to append zeros to the left until the 8 bits are completed. For example: 1101002  001101002 (8-bit number) 11110112  011110112 (8-bit number)

. Actually, you can use this method to convert a Number in Number in decimal number into any other base. For example, if base 10 base 8 you want to convert it into a base-8 number, just divide by 8 and group the remainders. 83 ????8 Remainder . Example: Converting a decimal number to base-8: 10 3 8 83

1 2 1238 8 10

0 1 8 1 stop here!

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

Exercise: . Convert the following two decimal numbers to binary numbers. Fill in the blanks in the figure below. Number in Number in Number in Number in base 10 base 2 base 10 base 2

63 ????2 97 ????2

Remainder Remainder

2 2 63 97

2 2

2 2

2 2

2 2

2 2

2

. Now, convert the following decimal numbers to binary numbers. The final binary number must have 8 bits (append zeros to the left to complete).

Binary number with 8 bits. Decimal number b7b6b5b4b3b2b1b0

40

255

111

126

9

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

HEXADECIMAL NUMBERS . In the decimal number system, a digit can take a value from 0 to 9. . In the hexadecimal number system, a hexadecimal digit can take a value from 0 to 15. However, as to avoid confusion, the numbers from 10 to 15 are represented by letters (A-F):

HEXADECIMAL DIGIT

0 1 2 3 4 5 6 7 8 9 A B C D E F

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

. A single hexadecimal digit is also called a nibble. . The following figure shows a 2-digit hexadecimal number. Note that the positional representation with powers of 16 let us obtain the decimal value (integer positive) of the hexadecimal number. This is the same as converting a hexadecimal number into a decimal number.

Hex. number Powers of 16: Decimal 1 0 Value MATHEMATICAL REPRESENTATION h1h0 h1 16 + h0 16 2-hexadecimal digits 5A 5 161 + A 160 90 h1 h0 10 1 161 + 0 160 16 08 0 161 + 8 160 8 1 0 Second Digit First Digit FB F 16 + B 16 251 1 0 3E 3 16 + E 16 62 1 0 A7 A 16 + 7 16 167

. Note that when we use the letters A-F in the multiplications inside the powers of 16 representation (e.g., A161 +7160), we need to replace the hexadecimal symbol by its decimal value. A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

For example: A161  (10)161.

EXERCISE: Convert the following hexadecimal numbers (positive integers) to their decimal values:

2-hex. digit 1 0 Decimal Number representation h116 +h016

AB

CE

05

70

F0

E9

General case: . Positional number representation for a hexadecimal number with ‘n’ nibbles (hexadecimal digits):

hn-1hn-2 ... h1h0 Most significant Least significant (leftmost) nibble (rightmost) nibble

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

. To convert a hexadecimal number into a decimal, we apply the following formula:

Decimal Value (integer positive): n-1 n-2 1 0 D = hn-1 16 + hn-2 16 + ... + h1 16 + h0 16

. To avoid confusion, it is sometimes customary to append the prefix ‘0x’ to a hexadecimal number:

0hn-1hn-2...h1h0 . Examples: FD0A90: 0FD0A90  F165 + D164 + 0163 + A162 + 9161 + 0160 0B871C: 00B871C  0165 + B164 + 8163 + 7162 + 1161 + C160

. The table presents the maximum attainable value for the given number of nibbles (hexadecimal digits). What pattern do you find? Can you complete it for the highlighted cases (4 and 6)?

Number of bits Maximum value Range 1 F  161-1 0  F  0  161-1 2 FF  162-1 0  FF  0  162-1 3 FFF  163-1 0  FFF  0  163-1 4

5 5 5 FFFFF  16 -1 0  111112  0  16 -1 6 … n FFF…FFF  16n-1 0  FFF…FFF  0  16n-1

. Maximum value for ‘n’ nibbles: The maximum decimal value with ‘n’ nibbles is given by:

D = FFF...FFF = F 16n-1 + F 16n-2 + ... + F 161 + F 160 15 16n-1 + 15 16n-2 + ... + 15 161 + 15 160 = 16n-1 n nibbles

 With ‘n’ nibbles, we can represent positive integer numbers from 0 to 16n-1. (16n numbers)

 Bytes, KB (Kilobytes), MB (Megabytes), GB (Gigabytes), TB (Terabytes)

Nibble Byte KB MB GB TB 4 bits 8 bits 1000 bytes 106 bytes 109 bytes 1012 bytes

Note that the nibble (4 bits) is one hexadecimal digit. Also, one byte (8 bits) is represented by two hexadecimal digits.

RELATIONSHIP BETWEEN HEXADECIMAL AND BINARY NUMBERS

. Conversions between hexadecimal and binary systems are very common when dealing with digital computers. In this activity, we will learn how these 2 systems are related and how easy it is to convert between one and the other.

. Hexadecimal to binary: We already know how to convert a hexadecimal number into a decimal number. We can then can convert the decimal number into a binary number (using successive divisions).

. Binary to hexadecimal: We can first convert the binary number to a decimal number. Then, using an algorithm similar to the one that converts into binary, we can convert our decimal number into a hexadecimal number.

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

SIMPLE METHOD TO CONVERT HEXADECIMAL TO BINARY NUMBERS AND VICEVERSA

. The previous two conversion processes are too tedious. Fortunately, hexadecimal numbers have an interesting property that allows quick conversion of binary numbers to and viceversa.

. Binary to hexadecimal: We group the binary numbers in groups of 4 (starting from the rightmost bit). If the last group of bits does not have four bits, we complete with zeros to the left. Then, we independently convert each group of 4 bits to its decimal value. Notice that 4 bits can only take decimal values between 0 and 24-1  0 to 15, hence 4 bits represent only one hexadecimal digit. In other words, for each group of 4 bits, there are only 16 possible hexadecimal digits to pick from. The figure below shows an example. binary dec hex Binary: 10111012 0101 1101 0000 0 0 decimal: 5 13 0001 1 1 0010 2 2 0011 3 3

hexadecimal: 5 D 0100 4 4 0101 5 5

0110 6 6

0111 7 7

Then: 010111012 = 0x5D 1000 8 8

1001 9 9 1010 10 A Verification: 1011 11 B 1100 12 C 6 4 3 2 0 010111012 = 1 2 + 1 2 + 1 2 + 1 2 + 1 2 = 93 1101 13 D

0x5D = 5 161 + D 160 = 93 1110 14 E 1111 15 F

. Exercise: Group the following binary numbers in groups of 4 bits and obtain the hexadecimal representation. Use the table in the previous figure to pick the correspondent hexadecimal digit for each group of 4 bits.

Binary number Hexadecimal number

1101111

101

1110

1100011

11111110

100001

. Hexadecimal to binary: It is basically the inverse of the process of converting a binary into a hexadecimal numbers. We pick each hexadecimal digit and convert it (using always 4 bits) to its 4- bit binary representation. The binary number is the concatenation of all resulting group of 4 bits.

FA C1

1111 1010 1100 0001

0xFA = 111110102 DO NOT discard these zeros 0xC1 = 110000012 when concatening!

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

Exercise: . Convert these hexadecimal numbers to binary. Verify it by converting both the binary and hexadecimal number (they should match).

Hexadecimal Binary number Decimal value number

A10

891

43

A2

FACE

. The reason hexadecimal numbers are popular is because hexadecimal numbers provide a short-hand notation for binary numbers.

. Another common number system is computers is base-8 (octal). The conversion between base-8 and base-2 resembles that of converting between base-16 and base-2. Here, we group the binary numbers in groups of 3 bits:

BINARY TO OCTAL binary dec oct Binary: 10111012 001 011 101 000 0 0 001 1 1 octal: 010 2 2 1 3 5 011 3 3 100 4 4 101 5 5 110 6 6 Then: 01011101 = 135 2 8 111 7 7

Verification:

6 4 3 2 0 010111012 = 1 2 + 1 2 + 1 2 + 1 2 + 1 2 = 93

2 1 0 1358 = 1 8 + 3 8 + 5 8 = 93

OCTAL TO BINARY

758 318

111 101 011 001

758 = 1111012 DO NOT discard these zeros 318 = 0110012 when concatening!

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

INTRODUCTION TO FIXED-POINT . We have been representing positive integer numbers. But what if we wanted to represent numbers with fractional parts? . Fixed-point arithmetic: Binary representation of positive decimal numbers with fractional parts.

Given the following binary number: (푏푛−1푏푛−2 … 푏1푏0. 풃−ퟏ풃−ퟐ … )2 Formula to convert it to decimal: 푛−1 푛−2 1 0 −ퟏ −ퟐ 퐷 = 푏푛−1 × 2 + 푏푛−2 × 2 + ⋯ + 푏1 × 2 + 푏0 × 2 + 풃−ퟏ × ퟐ + 풃−ퟐ × ퟐ + ⋯

Example: 3 2 1 0 −ퟏ −ퟐ −ퟑ (1011.101)2 = 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2 + 1 × ퟐ + ퟎ × ퟐ + ퟏ × ퟐ

To convert it to hexadecimal (or octal):

Binary: 1101.112 001 101.110

octal: 1 5 . 6

Binary: 10101.101012 0001 0101.1010 1000

hexadecimal: 1 5 . A 8

Now, what if we have a decimal number with fractional part? What we do is we divide the integer part and the fractional part. We obtain the binary representation of the integer part using what we know. As for the fractional part, what we do is successive multiplications by 2, the integer parts resulting are the results.

Example:

Number in Number in Number in Number in base 10 base 2 base 10 base 2

0.625 ????2 0.7 ????2 MSB MSB 0.625x2 = 1.25 = 1 + 0.25 0.7x2 = 1.4 = 1 + 0.4

0.25x2 = 0.5 = 0 + 0.5 0.4x2 = 0.8 = 0 + 0.8

0.5x2 = 1 = 1 + 0 0.8x2 = 1.6 = 1 + 0.6

0.1012 stop here! 0.6x2 = 1.2 = 1 + 0.2

0.2x2 = 0.4 = 0 + 0.4

0.4x2 = 0.8 = 0 + 0.8

0.10110 0110 ...2

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

BINARY CODES:

. We know that with ‘n’ bits, we can represent 2푛 numbers, from 0 푡표 2푛 − 1. . However, with ‘n’ bits, we can also represent 2푛 numbers in any range. Moreover, with ‘n’ bits we can represent 2푛 different symbols.

Example: 7-bit US-ASCII character-encoding scheme. Each character is represented by 7 bits. Thus, the number of characters that can be represented is given by 27 = 128.Each character is said to have a binary code.

Example: 24-bit color. Each color is represented by 24 bits, providing 224 distinct color. Each color is said to have a binary code.

. In general, we might have N symbols to represent. In this case, the number of bits needed to represent N symbols is given by ⌈푙표푔2푁⌉.

Example: N = 5 symbols. With 2 bits, only 4 symbols can be represented. With 3 bits, 8 symbols can be represented. Thus, the number of bits required is 푛 = 3 = ⌈푙표푔25⌉ = 푙표푔28. Note that 8 is the power of 2 closest to N=5 and that is greater than or equal to 5.

Example: Unicode can represent more than 110,000 characters and attempts to cover all world’s scripts. A common character encoding is UTF-16, which uses two pairs of 16-bit units. For most purposes, a 16- bit unit suffices (covering 216 = 65536) characters.

 : 03C0 : 03A9

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

BCD CODE: BCD decimal # Here, we represent decimal numbers by independently encoding each digit in 0 0000 binary form. Each digit requires 4 bits. 1 0001 . Notice that the binary numbers 1011 (10 ) to 1111(15) are not used. Only 2 0010 10 out of 16 values are used to encode every decimal digit. 3 0011 . This is a very useful code for inputting devices (e.g. keypad). However, for 4 0100 implementing arithmetic operations, the binary number representation is 5 0101 more efficient. Also note that 6 values are ‘wasted’. 6 0110 . The BCD code is not the same as the binary number!!! 7 0111 8 1000 Example: 58 9 1001 This decimal number can be represented as a binary number: 1110102 Using the BCD code, the binary representation would be: 01011000

. There exist many other binary codes (e.g., reflective gray code, 6-3-1-1 code, 2-out-of-5 code) to represent decimal numbers. Usually, each of them is tailored to an specific application.

Decimal REFLECTIVE GRAY CODE: g g g g g 1 0 Number b2b1b0 2 1 0 g3g2g1g0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 1 1 0 0 1 1 1 1 2 0 1 0 0 1 1 0 0 1 0 1 0 3 0 1 1 0 1 0 0 1 1 0 4 1 0 0 1 1 0 0 1 1 1 5 1 0 1 1 1 1 0 1 0 1 6 1 1 0 1 0 1 0 1 0 0 7 1 1 1 1 0 0 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1 0 How to convert from RGC to binary and binary to RGC? 1 0 1 1 1 0 0 1 1 0 0 0

Application Measuring angular position with 4-bit RGC. 4 beams are emitted along an axis. When a light beam passes (transparent spots, represented as whites), we get a logical 1, 0 otherwise. The RGC encoding makes that between areas only one bit changes, thereby reducing the possibility of a bad reading (especially when the beam between adjacent areas. Example: From 0001 to 0011: Only one bit flips. If we used 0001 to 0010, two bits would flip. This is prone to more errors, especially when the beams are close to the line where the two areas meet.

90 67.5 112.5 45 4

Angle g3g2g1g0 EMITTER

22.5 RECEPTOR 135 0 - 22.5 0 0 0 0 22.5 - 45 0 0 0 1 45 - 67.5 0 0 1 1 67.5 -90 0 0 1 0 157.5 0 90 - 112.5 0 1 1 0 112.5 - 135 0 1 1 1 135 - 157.5 0 1 0 1

180 337.5 157.5 - 180 0 1 0 0 180 - 202.5 1 1 0 0 202.5 - 225 1 1 0 1 225 - 247.5 1 1 1 1 202.5 315 247.5 - 270 1 1 1 0

270 - 292.5 1 0 1 0 292.5 - 315 1 0 1 1 225 292.5 315 - 337.5 1 0 0 1 247.5 270 337.5 - 360 1 0 0 0 Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

PRACTICE EXERCISES 1. Convert the following binary numbers to i) decimal, ii) octal, and iii) hexadecimal  00110101  11000001  11010101  11111110  10001100  1011.1110  1101.001  101.1001

2. Represent the following decimal numbers in the binary system: . 124,511,500,200,120,64,128, 31, 19, 31.25, 31.47, 20.23

3. What is the minimum number of bits required to represent the following decimal numbers: . 127, 256, 1024, 63, 17, 49, 511, 20.625, 55.125, 31.5

4. We want to represent 100,000 different colors. What is the minimum number of bits required to represent those colors?

5. Represent the following decimal numbers in i) binary, ii) BCD, iii) and Reflective Gray Code  125  128  110  256  443  17  122  240

6. Convert the following binary numbers into its correspondent Reflective Gray Code representation.  10101011101  10101110010  11110101  01010110  111100001

7. Convert the following binary strings coded with Reflective Gray Code into the binary number system:  010101101  010101010  111111100  1110010  110010

8. We have 2 numpads. To open a lock, one must press the correct number on each one (e.g. 9 and 2) . BCD encoding is very useful here, because each decimal number is represented with 4 bits and we can quickly create a circuit that opens the lock. If we wanted to use the binary representation of the decimal with 2 digits, we would need to read the 8 lines, convert those 8 bits to the binary representation (a non-trivial task) and then perform the comparison.

?

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

KARNAUGH MAPS:

2 variables:

x y f x y f x x 0 1 y 0 0 m y 0 0 0 0 0 1 0 m0 m2 y y 0 1 m1 0 1 1 1 0 1 1 0 m2 1 0 1 m1 m3 y 1 1 0 y 1 1 m3 x x x x f = x'y + y'x = m +m 1 2 x y f x 0 0 1 y 1 0 y 0 1 1 1 0 0 1 1 y 1 1 1

x x f = x' + y

3 variables: x y z f x y z f xy xy 00 01 11 10 00 01 11 10 z z 0 0 0 m0 0 0 0 1 0 m0 m2 m6 m4 z 0 1 0 0 1 z 0 0 1 m1 0 0 1 1 0 1 0 m2 0 1 0 0 1 m1 m3 m7 m5 z 1 1 0 1 0 z 0 1 1 m3 0 1 1 0 1 0 0 m4 x y x y x y x y 1 0 0 1 x y x y x y x y 1 0 1 m5 1 0 1 0 x x x x 1 1 0 m6 1 1 0 0 1 1 1 m7 y y y 1 1 1 1 y y y f = x'y' + z'y' + xyz

xy xy 00 01 11 10 00 01 11 10 z z 0 1 1 1 1 z 0 1 1 1 1 z

1 0 0 0 1 z 1 1 0 0 1 z

x y x y x y x y x y x y x y x y

x x x x

y y y y y y f = xy' + z' f = y' + z'

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

4 variables: x y z w f xy 00 01 11 10 xy 0 0 0 0 m zw 00 01 11 10 0 zw 0 0 0 1 m 00 m0 m4 m12 m8 z w w 1 z 00 1 0 0 1 z w w 0 0 1 0 m2 z 01 m1 m5 m13 m9 z w 0 0 1 1 m3 w 01 0 1 1 0 z w 0 1 0 0 m w 4 11 m3 m7 m15 m11 z w 0 1 0 1 m5 z 11 0 1 1 0 z w

0 1 1 0 m6 10 m2 m6 m14 m10 z w w z 10 0 1 1 1 m7 1 0 0 1 z w w 1 0 0 0 m x y x y x y x y 8 x y x y x y x y 1 0 0 1 m9 x x 1 0 1 0 m10 x x 1 0 1 1 m11 y y y y y y 1 1 0 0 m12 1 1 0 1 m13 1 1 1 0 m14 f = y'w' + wy 1 1 1 1 m15

xy x y z w f 00 01 11 10 xy zw 00 01 11 10 0 0 0 0 1 zw 00 1 1 1 1 z w w z 0 0 0 1 0 00 1 0 0 0 z w w z 01 0 0 0 0 z w 0 0 1 0 1 01 0 1 0 1 z w w 0 0 1 1 0 w 11 1 0 0 1 z w 0 1 0 0 0 11 0 1 0 0 z w z 0 1 0 1 1 z 10 1 1 1 1 z w w 0 1 1 0 0 10 1 0 0 0 z w w 0 1 1 1 1 x y x y x y x y x y x y x y x y 1 0 0 0 0 x x 1 0 0 1 1 x x 1 0 1 0 0 y y y 1 0 1 1 0 y y y 1 1 0 0 0 f = y'z + w' 1 1 0 1 0 f = x'y'w' + x'yw + xy'z'w 1 1 1 0 0 1 1 1 1 0

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

REPRESENTATION OF SIGNED NUMBERS: Signed number: MSB always represents the sign. 1: negative, 0: positive. Representations: Sign-and-magnitude, 1's complement, 2's complement

1's COMPLEMENT: For a number 퐵 = 푏푛−1 … 푏1푏0, the 1's complement of B is given by a number 퐾 = 푘푛−1 … 푘1푘0. K represents '-B'. where 퐾 = (2푛 − 1) − 퐵, 푛 = # 표푓 푏푖푡푠 (푖푛푐푙푢푑푖푛푔 푠푖푔푛 푏푖푡) (2푛 − 1): largest unsigned number with 푛 bits.

푛−1 푛−1 푛−1 푖 푛 푖 푖 푛 ̅ ∑ 푘푖2 = (2 − 1) − ∑ 푏푖2 → ∑(푘푖 − 푏푖)2 = 2 − 1 → 푘푖 + 푏푖 = 1, ∀푖  푘푖 = 푏푖 푖=0 푖=0 푖=0

Example: 퐵 = 010012 = 9 (positive number in 1's complement)

To get '-9' in 1's complement representation, we just invert each bit of B: 퐾 = 101102 = −9 (negative number is 1's complement)

* Notice that 퐾 = (2푛 − 1) − 퐵 is valid only when we think of K and 5 B as unsigned numbers. 퐾 = 101102 = 22 = (2 − 1) − 9

Example:

sign andmagnitude sign 2's complement 2's b3b2b1b0 complement 1's 퐵 = 110102 (negative number in 1's complement) Remember: If the MSB is '1', the number is negative. 0 0 0 0 0 0 0 0 0 0 1 1 1 1 What number does it represent? We get the positive number first. 0 0 1 0 2 2 2 → 퐾 = 001012 = 5 Thus, B = -5. 0 0 1 1 3 3 3 0 1 0 0 4 4 4 The 1's complement representation allows us to represent both 0 1 0 1 5 5 5 positive and negative numbers. Note that to obtain the 1's 0 1 1 0 6 6 6 complement of a number, you need to invert the bits. Also, the 1's 0 1 1 1 7 7 7 complement of a number can be either a positive or negative 1 0 0 0 0 -7 -8 number. 1 0 0 1 -1 -6 -7 With 푛 bits, we can represent 2푛 − 1 numbers from −2푛−1 + 1 0 1 0 -2 -5 -6 1 푡표 2푛−1 − 1. When using the 1's complement representation, it is 1 0 1 1 -3 -4 -5 mandatory to specify how many bits we are using. 1 1 0 0 -4 -3 -4 1 1 0 1 -5 -2 -3 2's COMPLEMENT: For a number 퐵 = 푏 … 푏 푏 , the 2's complement of B is given by 1 1 1 0 -6 -1 -2 푛−1 1 0 1 1 1 1 -7 0 -1 a number 퐾 = 푘푛−1 … 푘1푘0. K represents '-B'. where 퐾 = 2푛 − 퐵, 푛 = # 표푓 푏푖푡푠 (푖푛푐푙푢푑푖푛푔 푠푖푔푛 푏푖푡) 퐾 = 2푛 − 퐵 = (2푛 − 1) − 퐵 + 1

Then, to get the 2's complement: first get the 1's complement and then add 1. Or in other words, first invert all the bits and then add 1.

Example: 퐵 = 010012 = 9 (positive number in 2's complement)

To get '-9' in 2's complement representation, we invert each bit of B and add 1: 퐾 = 101112 = −9 (negative number is 2's complement)

푛 5 * 퐾 = 2 − 퐵 is valid only if we think of K and B as unsigned numbers. 퐾 = 101112 = 23 = 2 − 9

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

Example: 퐵 = 110102 (negative number in 2's complement) Remember: If the MSB is '1', the number is negative.

What number does it represent? We get the positive number first. → 퐾 = 001102 = 6 Thus, B = -6.

With 푛 bits, we can represent 2푛 numbers from −2푛−1 푡표 2푛−1 − 1. When using the 2's complement representation, it is mandatory to specify how many bits we are using

Formula to get the decimal value of a number in 2's complement representation:

If the number B is positive, we know that the MSB is 0, 푏푛−1 = 0. 푛−1 푛−2 푛−2 푖 푛−1 푖 푖 퐵 = ∑ 푏푖2 = 푏푛−12 + ∑ 푏푖2 = ∑ 푏푖2 (푎) 푖=0 푖=0 푖=0

If the number B is negative, we know that the MSB is 1, 푏푛−1 = 1. If we take the 2's complement of B, we get K. The number K represents -B. The number K is positive. 퐾 = 2푛 − 퐵 is valid if we think of K and B as unsigned numbers: 푛−1 푛−1 푖 푛 푖 ∑ 푘푖2 = 2 − ∑ 푏푖2 푖=0 푖=0 We want a formula that involves 푏푖 and that provides the decimal number -K. 푛−1 푛−1 푛−2 푖 푖 푛 푛−1 푖 푛 −퐾 = − ∑ 푘푖2 = ∑ 푏푖2 − 2 = 푏푛−12 + ∑ 푏푖2 − 2 푖=0 푖=0 푖=0 푛−2 푛−2 푛−1 푖 푛 푛−1 푖 −퐾 = 푏푛−12 + ∑ 푏푖2 − 2 = 2 (푏푛−1 − 2) + ∑ 푏푖2 푖=0 푖=0 푛−2 푛−2 푛−1 푖 푛−1 푖 퐵 = −퐾 = 2 (1 − 2) + ∑ 푏푖2 = −2 + ∑ 푏푖2 (푏) 푖=0 푖=0

Using (a) and (b), the formula for the decimal value of B (either positive or negative) is: 푛−2 푛−1 푖 퐵 = −푏푛−12 + ∑ 푏푖2 푖=0 Examples: 4 2 1 101102 = −2 + 2 + 2 = −10 4 3 110002 = −2 + 2 = −8

Sign-extension in 2's complement representation: In many circumstances, we might want to represent numbers in 2's complement with a fixed number of bits. For example, the following two numbers require a minimum of 5 bits: 4 2 1 0 101112 = −2 + 2 + 2 + 2 = −9 3 2 1 0 011112 = 2 + 2 + 2 + 2 = +15

What if we wanted to use 8 bits to represent them? The 2's complement representation allows us to sign- extend: If the number is positive, we attach zeros to the left. If the number is negative, we attach ones to the left. In the example, note how we added three bits to the left in each case:

4 2 1 0 111101112 = −2 + 2 + 2 + 2 = −9 3 2 1 0 000011112 = 2 + 2 + 2 + 2 = +15

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

ARITHMETIC OPERATIONS

UNSIGNED NUMBERS - ADDITION

Addition of 2 bits: x + 0 + 0 + 1 + 1 + y 0 1 0 1

c s 0 0 0 1 0 1 1 0

carry sum

x y c s x s y 0 0 0 0 x s 0 1 0 1 HA y c 1 0 0 1 1 1 1 0 c

Addition of 5 bits: ci 15: 01111+ x4x3x2x1x0+ x + x s 10: 01010 y4y3y2y1y0 y y FA c c s ci o 25: 11001 c s4s3s2s1s0 o o carry sum

x s x s HA  y FA y c 0 co

There are 10 inputs. Building the truth table is not practical! Instead we build a cascade of Full Adders. General case: 'n' bits: xn-1 yn-1 x2 y2 x1 y1 x0 y0

cout cn cn-1 ... c3 c2 c1 c0 cin= 0 FA FA FA FA

s s s s n-1 2 1 0 Full adder design:

x y c x y c s i i i i i i+1 i 00 01 11 10 si = xiyici + xiyici + xiyici + xiyici ci 0 0 1 0 1 0 0 0 0 0 si = (xiyi)ci + (xiyi)ci 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 si = xiyici 0 1 1 1 0 1 0 0 0 1 xiyi 00 01 11 10 1 0 1 1 0 ci 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 ci+1 = xiyi + xici + yici 1 0 1 1 1

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

Overflow:

Suppose we have only 4 bits to represent binary numbers:

0101 + 1011 + 1001 0110

1110 10001 cout=0 cout=1  OVERFLOW!!!

Overflow occurs when an arithmetic operation require more bits than the bits we are using to represent our numbers. For 4 bits, the range is 0 to 15. If the summation is greater than 15, then there is overflow. For n bits, overflow occurs when the summation is greater than 2푛 − 1. To detect overflow: For unsigned numbers, the overflow bit is the same as the output cout.

To avoid overflow when adding, a common technique is to sign-extend the two 01011 + summands. For example, if the two summands are 4-bits wide, then we add 00110 one more bit. So, we use 5 bits to represent our numbers. In the case of unsigned numbers, sign-extend amounts to zero-extend. Note that in general, if the two summands are n-bits wide, the result will have at 10001 most n+1 bits. cout=0

SIGNED NUMBERS (2's COMPLEMENT) - ADDITION

+5 = 0101 + -5 = 1010 + +5 = 0101 + -5 = 1010 +

+2 = 0010 +2 = 0010 -2 = 1101 -2 = 1101

+7 = 0111 -3 = 1100 +3 =10011 -7 =11001

cout=0 cout=0 cout=1 cout=1 There is no overflow here, we just ignore the cout bit

We can use the same circuitry that we used for unsigned numbers. That's the great advantage of the 2's complement representation. x y x y x y x y 3 3 2 2 1 1 0 0

cout c4 c3 c2 c1 c0 cin= 0 FA FA FA FA

s3 s2 s1 s0 Overflow: For 4 bits, the range that we represent is −23 푡표 23 − 1. Overflow occurs when the summation falls outside this range.

cout = c 4 +7 = 0111 + -7 = 1001 + +7 = 0111 + -7 = 1001 + +2 = 0010 +2 = 0010 -2 = 1110 -2 = 1110

+9 = 1001 -5 = 1011 +5 =10101 -9 =10111

c4 = 0, c3 = 1 c4 = 0, c3 = 0 c4 = 1, c3 = 1 c4 = 1, c3 = 0

If 푐4 ≠ 푐3,  there is overflow. If 푐4 = 푐3,  No overflow and we ignore 푐4. 푂푣푒푟푓푙표푤 = 푐4푐3

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

For n bits, the range is −2푛−1 푡표 2푛−1 − 1. Overflow occurs when the summation falls outside this range. 푂푣푒푟푓푙표푤 = 푐푛푐푛−1

x3 y3 x2 y2 x1 y1 x0 y0 Circuit for n =4:

cout c4 c3 c2 c1 c0 cin= 0 FA FA FA FA

overflow

s3 s2 s1 s0 cout = c To avoid overflow when adding, a common technique is 5 to sign-extend the two summands. For example, if the +7 = 00111 + -7 = 11001 + two summands are 4-bits wide, then we add one more +2 = 00010 -2 = 11110 bit. So, we use 5 bits to represent our numbers. Note that in general, if the two summands are n-bits +9 = 01001 -9 = 10111 wide, the result will have at most n+1 bits. c = 0, c = 0 c = 1, c =1 Important: If there is no overflow, you MUST discard the 5 4 5 4 carry out, and only use the 'n' bits.

SUBTRACTION: X - Y = X + (-Y). 7 - 3 = 7 + (-3): +3=0011 -> -3=1101 In this case, we only need to create the 2's complement of Y (the subtrahend). cout = c4 +7 = 0111 + -3 = 1101 The circuit is basically the same as the adder. We just need to include a circuit that creates the 2's complement of Y: +4 = 0100

x y x y x y x y 3 3 2 2 1 1 0 0 c = 1, c = 1 4 3

cout c4 c3 c2 c1 c0 cin= 1 FA FA FA FA overflow s s s s 3 2 1 0 Adder/Subtractor Unit for 2's complement numbers: x3 y3 x2 y2 x1 y1 x0 y0 add/sub

add =0 sub = 1

cout c4 c3 c2 c1 c0 cin FA FA FA FA overflow

s3 s2 s1 s0

add/sub yi f

0 0 0 add/sub f 0 1 1 y 1 0 1 i 1 1 0

Instructor: Daniel Llamocca DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013

PRACTICE EXERCISES 1. Complete the following table. Use the fewest number of bits for every case.

Decimal Sign-and-magnitude 1's complement 2's complement -129 256 123 78 -67 -1024 62 -92 -888 434

2. Perform the following operations in 2's complement. Use the fewest number of bits for the operation, sign-extend when needed.  45 + 50  127 + 2  112 - 39  -160 - 97  -120 - 121  -98 + 1

3. Convert the following numbers to its 2's complement representation. But you need to use 16 bits to represent each number.  -1023  98  512  197  -128  789

4. We want to perform the following addition/subtraction operations. We are using the 2's complement representation. However, we are restricted to use only 10 bits. Determine whether there is overflow when performing these operations only using the decimal numbers.  -510 - 20  -250 - 270  400 + 300  -200 - 195  191 + 350  390 + 122

5. We want to perform the following operation using the 2's complement representation. First, determine the number of bits that we require to represent the two summands. Then determine whether there is overflow by performing the operation in 2's complement. Get cn, cn-1 (carries) on each case.  23+45  -127-120  -23+46  490 + 50  -129+128  990+113

6. The following is the formula to get the decimal value that a number in 2's complement represents. 푛−2 푛−1 푖 퐵 = −푏푛−12 + ∑ 푏푖2 푖=0 Derive a formula to get the decimal value that a number in 1's complement represents.

Instructor: Daniel Llamocca