UNSIGNED INTEGER NUMBERS Notes - Unit 4 DECIMAL NUMBER SYSTEM A decimal digit can take values from to 9: Digit-by-digit representation of a positive integer number (powers of ): DIGIT 3 4 5 6 7 8 9 Number: 937 hundreds thousands tens units 9 3 7 9 thousands, 3 hundreds, 7 tens, and units 3 937 = 9 3 + 3 + 7 + POSITIONAL NUMBER REPRESENTATION Let s consider the numbers from to 999. We represent these numbers with 3 digits (each digit being a number between and 9). We show a 3-digit number using the positional number representation: MATHEMATICAL REPRESENTATION 3-digit d d d EXAMPLE 9 Third Digit Second Digit First Digit The positional number representation allows us to express the decimal value using powers of ten: d + d + d. Example: Decimal Number 9 5 9 8 55 Exercise: Write down the 3-digit and the powers of ten representations for the following numbers: Decimal Number 3-digit representation d + d + d 5 54 99 3-digit representation d d d 9 5 9 8 55 Third Digit Second Digit First Digit Powers of : d + d + d + + + + 9 + + + + 5 + 9 + + + 8 + 5 + 5 Instructor: Daniel Llamocca
General Case: Positional number representation for an integer positive number with n digits: Decimal Value: D = i=n d i i = d n n + d n n + + d + d i= 6 5 4 3 Example: 9834 (7 digits). 9834 9 8 3 4 3476 (6 digits). 5 4 3 3476 3 4 7 6 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 9 = - 9-99 = - 99-3 999 = 3-999 3-4 5 99999 = 5-99999 5-6 d n- d n- d d n 999 999 = n - 999 999 n - 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: D = 999999 = 9 n- + 9 n- + + 9 + 9 = n - n digits With n digits, we can represent n positive integer numbers from to n -. With 7 digits, what is the range (starting from ) 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, there exist other number systems: octal, hexadecimal, vigesimal, binary, etc. In particular, binary numbers are very practical as they are used by digital computers. For binary numbers, the counterpart of the decimal digit (that can take values from to 9) is the binary digit, or bit (that can take the value of or ). DIGIT BIT 3 4 5 6 7 8 9 Bit: Unit of information that a computer uses to process and retrieve data. It can also be used as a Boolean variable (see Unit ). Binary number: This is represented by a string of bits using the positional number representation: b n b n b b Converting a binary number into a decimal number: The following figure depicts two cases: -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
MATHEMATICAL REPRESENTATION -bit Second Bit b b First Bit Binary number Powers of : Decimal Number b b b + b + + + + 3 MATHEMATICAL REPRESENTATION 3-bit b b b Third Bit Second Bit First Bit Binary number General case: Positional number representation for a binary number with n bits: Powers of : Decimal Number b b b b + b + b + + + + + + + + + + + + + + + + 3 4 5 6 7 Most significant (leftmost) bit b n- b n- b b Least significant (rightmost) bit The binary number can be converted to a positive decimal number by using the following formula: D = i=n b i i = b n n + b n n + + b + b i= To avoid confusion, we usually write a binary number and attach a suffix : (b n b n b b ) Example: 6 bits: () D = 5 + 4 + 3 + + + = 43 4 bits: () D = 3 + + + = Maximum value for a given number of bits. Complete the tables for the highlighted cases (4 and 6): Number of bits Maximum value Range - - - - 3 3-3 - 4 5 5-5 - 6 n n - n - Maximum value for n bits: The maximum binary number is given by an n-bit string of s:. Then, the maximum decimal numbers is given by: D = = n- + n- + + + = n - n bits With n bits, we can represent n positive integer numbers from to n -. 3 Instructor: Daniel Llamocca
The case n=8 bits is of particular interest, as a string of 8 bits is called a byte. For 8-bit numbers, we have 56 numbers in the range to 8 - to 55. Most significant (leftmost) bit b 7 b 6 b 5 b 4 b 3 b b b Least significant (rightmost) bit The table shows some examples: Decimal Number 8-bit format b 7 b 6 b 5 b 4 b 3 b d d b 7 7 + b 6 6 + b 5 5 + b 4 4 + b 3 3 + b + b + b 9 5 9 8 55 7 + 6 + 5 + 4 + 3 + + + 7 + 6 + 5 + 4 + 3 + + + 7 + 6 + 5 + 4 + 3 + + + 7 + 6 + 5 + 4 + 3 + + + 7 + 6 + 5 + 4 + 3 + + + 7 + 6 + 5 + 4 + 3 + + + 7 + 6 + 5 + 4 + 3 + + + Exercise: Convert the following binary numbers (positive integers) to their decimal values: 8-bit representation b 7 7 + b 6 6 + b 5 5 + b 4 4 + b 3 3 + b + b + b Decimal Number CONVERSION OF A NUMBER IN ANY BASE TO THE DECIMAL SYSTEM To convert a number of base 'r' (r =, 3,4, ) to decimal, we use the following formula: base 'r': (r n r n r r ) r Conversion to decimal: D = i=n r i r i = r n r n + r n r n + + r r + r r i= Also, the maximum decimal value for a number in base 'r' with 'n' digits is: D = rrr rrr = r r n + r n r n + + r r + r r = r n Example: Base-8: Number of digits Maximum value Range 7 8 8-7 8 8-77 8 8-77 8 8-3 777 8 8 3-777 8 8 3 - n 777 777 8 8 n - 777 777 8 8 n - Examples: (563) 8 : base 8 (octal system) Number of digits: n = 5 Conversion to decimal: D = 5 8 4 + 8 3 + 6 8 + 3 8 + 8 = 89 (3) 4 : base 4 (quaternary system) Number of digits: n = 4 Conversion to decimal: D = 3 4 3 + 4 + 4 + 4 = 4 Instructor: Daniel Llamocca
CONVERSION OF DECIMAL (INTEGER POSITIVE) TO BINARY NUMBERS Examples: base base base base 5???? 3???? 6 5 Remainder 6 3 Remainder 3 6 3 6 6 3 5 3 3 6 7 5 3 3 7 3 stop here! 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: (8-bit number) (8-bit number) Actually, you can use this method to convert a decimal number into any other base. For example, if you want to convert it into a base-8 number, just divide by 8 and group the remainders. Example: Converting a decimal number to base-8: base 8 83 83 Remainder 3 base 8???? 8 8 3 8 8 stop here! 5 Instructor: Daniel Llamocca
Exercise: Convert the following two decimal numbers to binary numbers. Fill in the blanks in the figure below. base base base base 63???? 97???? Remainder Remainder 63 97 Now, convert the following decimal numbers to binary numbers. The final binary number must have 8 bits (append zeros to the left to complete). Decimal number 4 Binary number with 8 bits b 7b 6b 5b 4b 3b b b 55 6 9 6 Instructor: Daniel Llamocca
HEXADECIMAL NUMBER SYSTEM This is a very useful system as it is a short-hand notation for binary numbers In the decimal number system, a digit can take a value from to 9. A hexadecimal digit is also called a nibble. A hexadecimal digit can take a value from to 5. To avoid confusion, the numbers to 5 are represented by letter (A-F): Hexadecimal digits 3 4 5 6 7 8 9 A B C D E F 3 4 5 6 7 8 9 3 4 5 The following figure shows a -digit hexadecimal number. Note that the positional representation with powers of 6 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. MATHEMATICAL REPRESENTATION -hexadecimal digits Second Digit h h First Digit Decimal digits Hex. number Powers of 6: h h h 6 + h 6 5A 5 6 + A 6 9 6 + 6 6 8 6 + 8 6 8 FB F 6 + B 6 5 3E 3 6 + E 6 6 A7 A 6 + 7 6 67 Decimal Value Note that when we use the letters A-F in the multiplications inside the powers of 6 representation (e.g., A 6 +7 6 ), we need to replace the hexadecimal symbol by its decimal value. A =, B =, C =, D = 3, E = 4, F = 5 For example: A 6 () 6. EXERCISE: Convert the following hexadecimal numbers (positive integers) to their decimal values: -hex. digit representation AB h 6 +h 6 Decimal Number CE 5 7 F E9 General case: Positional number representation for a hexadecimal number with n nibbles (hexadecimal digits): Most significant (leftmost) nibble h n- h n- h h Least significant (rightmost) nibble 7 Instructor: Daniel Llamocca
To convert a hexadecimal number into a decimal, we apply the following formula: Decimal Value (integer positive): D = i=n h i 6 i = h n 6 n + h n 6 n + + h 6 + h 6 i= To avoid confusion, it is sometimes customary to append the prefix x to a hexadecimal number: Examples: FDA9: FDA9 F 6 5 + D 6 4 + 6 3 + A 6 + 9 6 + 6 B87C: B87C 6 5 + B 6 4 + 8 6 3 + 7 6 + 6 + C 6 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 nibbles Maximum value Range F 6 - F 6 - FF 6 - FF 6-3 FFF 6 3 - FFF 6 3-4 5 FFFFF 6 5 - FFFFF 6 5-6 h n- h n- h h n FFF FFF 6 n - FFF FFF 6 n - Maximum value for n nibbles: The maximum decimal value with n nibbles is given by: D = FFFFFF = F 6 n- + F 6 n- + + F 6 + F 6 5 6 n- + 5 6 n- + + 5 6 + 5 6 = 6 n - n nibbles With n nibbles, we can represent positive integer numbers from to 6 n -. (6 n numbers) UNITS OF INFORMATION Nibble Byte KB MB GB TB 4 bits 8 bits bytes bytes 3 bytes 4 bytes Note that the nibble (4 bits) is one hexadecimal digit. Also, one byte (8 bits) is represented by two hexadecimal digits. While KB, MB, GB, TB (and so on) should be powers of in the International System, it is customary in digital jargon to use powers of to represent them. In microprocessor systems, memory size is usually a power of due to the fact that the maximum memory size is determined by the number of addresses the address bus can handle (which is a power of ). As a result, it is very useful to use the definition provided here for KB, MB, GB, TB (and so on). Digital computers usually represent numbers utilizing a number of bits that is a multiple of 8. The simple hexadecimal to binary conversion may account for this fact as we can quickly convert a string of bits that is a multiple of 8 into a string of hexadecimals digits. The size of the data bus in a processor represents the computing capacity of a processor, as the data bus size is the number of bits the processor can operate in one operation (e.g.: 8-bit, 6-bit, 3-bit processor). This is also usually expressed as a number of bits that is a multiple of 8. 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 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 decimals into binary, we can convert our decimal number into a hexadecimal number. 8 Instructor: Daniel Llamocca
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 hexadecimals 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 append 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 and 4 - to 5, hence 4 bits represent only one hexadecimal digit. In other words, for each group of 4 bits, there are only 6 possible hexadecimal digits to pick from. The figure below shows an example. Binary: decimal: hexadecimal: Then: Verification: = x5d 5 3 5 D = 6 + 4 + 3 + + = 93 x5d = 5 6 + D 6 = 93 binary dec hex 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A B C 3 D 4 E 5 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 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 (always using 4 bits) to its 4-bit binary representation. The binary number is the concatenation of all resulting group of 4 bits. C x = xc = DO NOT discard these zeros when concatening! 9 Instructor: Daniel Llamocca
Exercise: Convert these hexadecimal numbers to binary. Verify it by converting both the binary and hexadecimal number (they should match). Hexadecimal number A 89 43 A CE Binary number Decimal value The reason hexadecimal numbers are popular is because hexadecimal numbers provide a short-hand notation for binary numbers. OCTAL NUMBERS An octal digit can takes between and 7. This is another common number system in computers is base-8 (octal). The conversion between base-8 and base- resembles that of converting between base-6 and base-. Here, we group binary numbers in 3-bit groups: BINARY TO OCTAL Binary: octal: 3 5 Then: = 35 8 binary dec oct 3 3 4 4 5 5 6 6 7 7 Verification: = 6 + 4 + 3 + + = 93 35 8 = 8 + 3 8 + 5 8 = 93 OCTAL TO BINARY 75 8 3 8 75 8 = 3 8 = DO NOT discard these zeros when concatening! Instructor: Daniel Llamocca
APPLICATIONS OF BINARY AND HEXADECIMAL REPRESENTATIONS INTERNET PROTOCOL ADDRESS (IP ADDRESS): Hexadecimal numbers represent a compact way of representing binary numbers. The IP address is defined as a 3-bit number, but it is displayed as a concatenation of four decimal values separated by a dot (e.g., 9.6.53.76). The following figure shows how a 3-bit IP address expressed as a binary number is transformed into the standard IP address notation. IP address (binary): Conversion to hexadecimal: IP address (hex): x8a354c IP address notation: 9.6.53.76 8 A 3 5 4 C 9 6 53 76 Grab pairs of hexadecimal numbers and convert each of them to decimal. The 3-bit IP address expressed as binary number is very difficult to read. So, we first convert the 3-bit binary number to a hexadecimal number. The IP address expressed as a hexadecimal (x8a354c) is a compact representation of a 3-bit IP address. This should suffice. However, it was decided to represent the IP address in a 'human-readable' notation. In this notation, we grab pairs of hexadecimal numbers and convert each of them individually to decimal numbers. Then we concatenate all the values and separate them by a dot. Important: Note that the IP address notation (decimal numbers) is NOT the decimal value of the binary number. It is rather a series of four decimal values, where each decimal value is obtained by independently converting each two hexadecimal digits to its decimal value. Given that each decimal number in the IP address can be represented by hexadecimal digits (or 8 bits), what is the range (min. value, max. value) of each decimal number in the IP address? With 8 bits, we can represent 8 = 56 numbers from to 55. An IP address represents a unique device connected to the Internet. Given that the IP address has 3 bits (or 8 hexadecimal digits), the how many numbers can be represented (i.e., how many devices can connect to the Internet)? 3 = 49496796 devices. The number of devices that can be connected to the Internet is huge, but considering the number of Internet-capable devices that exists in the entire world, it is becoming clear that 3 bits is not going to be enough. That is why the Internet Protocol is being currently extended to a new version (IPv6) that uses 8 bits for the addresses. With 8 bits, how many Internet-capable devices can be connected to the Internet? 8 3.4 38 devices REPRESENTING GRAYSCALE PIXELS A grayscale pixel is commonly represented with 8 bits. So, a grayscale pixel value varies between and 55, being the darkest (black) and 55 being the brightest (white). Any value in between represents a shade of gray. 55 MEMORY ADDRESSES The address bus size in processors is usually determined by the number of memory positions it can address. For example, if we have a microprocessor with an address bus of 6 bits, we can handle up to 6 addresses. If the memory content is one byte wide, then the processor can handle up to 6 bytes = 64KB. Here, we use 6 bits per address, or 4 nibbles. The lowest address (in hex) is x and highest address (in hex) is xffff. Address : x : x : xffff 8 bits Instructor: Daniel Llamocca
Examples: A microprocessor can only handle memory addresses from x to x7fff. What is the address bus size? If each memory position is one byte wide, what is the maximum size (in bytes) of the memory that we can connect? 8 bits Address We want to cover all the cases from x to x7fff: The range from x to x7fff is akin to all possible cases with 5 bits. Thus, the address bus size is 5 bits. We can handle 5 bytes = 3KB of memory. : x : x : x7fff A microprocessor can only handle memory addresses from x to x3fff. What is the address bus size? If each memory position is one byte wide, what is the maximum size (in bytes) of the memory that we can connect? 8 bits Address We want to cover all the cases from x to x3fff: : x The range from x to x3fff is akin to all possible cases : x with 4 bits. Thus, the address bus size is 4 bits. We can handle 4 bytes = 6KB of memory. : x3fff A microprocessor has a 4-bit address line. We connect a memory chip to the microprocessor. The memory chip addresses are assigned the range x8 to xbfffff. What is the minimum number of bits required to represent addresses in that individual memory chip? If each memory position is one byte wide, what is the memory size (in bytes)? By looking at the binary numbers from x8 to xbfffff, we notice that the addresses in that range require 4 bits. But all those addresses share the same first two MSBs:. Thus, if we were to use only that memory chip, we do not need those bits, and we only need bits. We can handle bytes = 4MB of memory. Address : x8 : x8 : xbfffff A memory has a size of 5KB, where each memory content is 8-bits wide. How many bits do we need to address the contents of this memory? Recall that: 5KB = 9 bytes. So we need 9 bits to address the contents of this memory (address bus size = 9 bits) In general, for a memory with N address positions, the number of bits to address those positions is given by: log N 8 bits A -bit address line in a microprocessor with an 8-bit data bus handles MB ( bytes) of data. We want to connect four 56 KB memory chips to the microprocessor. Provide the address ranges that each memory device will occupy. Address : x : x : x3ffff 8 bits 56KB Each memory chip can handle 56KB of memory. 56KB = 8 bytes, requiring 8 bits for its address. For a -bit address: we have 5 hexadecimal digits that go from x to xfffff ( memory positions). We divide the memory positions into 4 contiguous groups, each with 8 memory positions. The figure shows the optimal way of doing so: for each group, the 8 LSBs of the memory addresses correspond to the memory range of a 56 KB memory. And the MSBs of the memory addresses are the same within a group. For a given memory address, we can quickly determine which group it belongs to by looking at its MSBs. : x4 : x4 : x7ffff : x8 : x8 : xbffff : xc : xc : xfffff 3 4 56KB 56KB 56KB Instructor: Daniel Llamocca
c5= c4= c3= c= c= c= c8= c7= c6= c5= c4= c3= c= c= c= c= c= c= c8= c7= c6= c5= c4= c3= c= c= c= c= c= c= UNSIGNED NUMBERS - ADDITION In the example, we add two 8-bit numbers using binary representation and hexadecimal representation (this is a short-hand notation). Note that every summation of two digits (binary or hexadecimal) generates a carry when the summation requires more than one digit. Also, note that c is the carry in of the summation. c is usually zero. The last carry (c 8 when n=8) is the carry out of the summation. If it is zero, it means that the summation can be represented with 8 bits. If it is one, it means that the summation requires more than 8 bits (in fact 9 bits); this is called an overflow. In the example, we add two numbers and overflow occurs: an extra bit (in red) is required to correctly represent the summation. x3f = + xb = xf = x3f = + xc = 3 F + B F 3 F + C DIGITAL CIRCUIT IMPLEMENTATION -bit Addition: Addition of a bit without carry in: The circuit that implements this operation is called Half Adder (HA). Boolean Algebra is a very powerful tool for the implementation of digital circuits. Note how the -bit addition operation x + y was mapped into Boolean equations for c and s. c = x.y, s = x y, where x, y, c, s are Boolean variables. Addition of a bit with carry in: The circuit that performs this operation is called Full Adder (). carry out carry out x y c o c i x + y s c s c s sum x + y x y x y c i sum + s c o x y + s c HA x y s c + x y HA + s c s c o n-bit Addition: The figure on the right shows a 5-bit addition. Using the truth table method, we would need inputs and 6 outputs. This is not practical! Instead, it is better to build a cascade of Full Adders. For an n-bit addition, the circuit is: c out c in x n- x n- x x + y n- y n- y y c out x n- y n- c n c n- c 3 5: + : x y 5: c x y c c out c in x 4 x 3 x x x + y 4 y 3 y y y s 4 s 3 s s s x y c c in s n- s n- s s Full Adder Design x i y i c i c i+ s i s n- s s s x i y i c i s i = x i y i c i + x i y i c i + x i y i c i + x i y i c i s i s i = (x i y i )c i + (x i y i )c i = x i y i c i x i y i c i c i+ = x i y i + x i c i + y i c i 3 Instructor: Daniel Llamocca
b 8 = b 7 = b 6 = b 5 = b 4 = b 3 = b = b = b = b = b = b = b 8 = b 7 = b 6 = b 5 = b 4 = b 3 = b = b = b = b = b = b = ARITHMETIC OVERFLOW: Suppose we have only 4 bits to represent binary numbers. 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 to 5. If the summation is greater than 5, then there is overflow. + cout= cout= + OVERFLOW!!! For n bits, overflow occurs when the sum is greater than n. Also: overflow = c n = c out. To avoid overflow in addition operation, a common technique is to sign-extend the two summands. For example, if the two summands are 4-bits wide, then we add one more bit. So, we use 5 bits to represent our numbers. In the case of unsigned numbers, sign-extend amounts to zero-extend. cout= In general, if the two summands are n-bits wide, the result will have at most n + bits ( n + n = n ). + x n- y n- x y x y x y c n c n- c 3 c c c c in overflow=c out s n- s s s UNSIGNED NUMBERS - SUBTRACTION In the example, we subtract two 8-bit numbers using the binary and hexadecimal (this is a short-hand notation) representations. A subtraction of two digits (binary or hexadecimal) generates a borrow when the difference is negative. So, we borrow from the next digit so that the difference is positive. Recall that a borrow in a subtraction of two digits is an extra that we need to subtract. Also, note that b is the borrow in of the summation. This is usually zero. The last borrow (b 8 when n=8) is the borrow out of the subtraction. If it is zero, it means that the difference is positive and can be represented with 8 bits. If it is one, it means that the difference is negative and we need to borrow from the next digit. In the example, we subtract two 8-bit numbers, the result we have borrows from the next digit. x3a = - xf = xb = x3a = - x75 = 3 A - F B 3 A - 7 5 xc5 = C 5 We can build an n-bit subtractor for unsigned numbers using Full Subtractor circuits. Subtraction for unsigned numbers only makes sense if the result is positive (or when doing multi-precision subtraction). In practice, subtraction is better performed in the s complement representation (for signed numbers). x n- y n- x y x y x y b out b in x n- x n- x x - y n- y n- y y b out b n b n- FS b 3 FS b FS b FS b b in d n- d n- d d Full Subtractor Design x i y i b i b i+ d i d n- d d d x i y i b i d i = x i y i b i + x i y i b i + x i y i b i + x i y i b i x i y i b i d i = (x i y i )b i + (x i y i )b i d i = x i y i b i b i+ = x i y i + x i b i + y i b i 4 Instructor: Daniel Llamocca
SIGNED INTEGER NUMBERS For an n-bit number b n b n b b, there exist three common signed representations: sign-and-magnitude, s complement, and s complement. In these three representations, the MSB always tells us whether the number is positive (MSB=) or negative (MSB=). These representations allow us to represent both positive and negative numbers. SIGN-AND-MAGNITUDE (SM): Here, the sign and the magnitude (value) are represented separately. The MSB only represents the sign and the remaining n bits the magnitude. Example (n=4): = +6 = -6 'S COMPLEMENT (C): Here, if the MSB=, the number is positive and the remaining n bits represent the magnitude. If the MSB=, the number is negative and the remaining n bits do not represent the magnitude. To invert the sign of a number in s complement representation, we apply the s complement operation to the number, which consists of inverting all the bits. Let B = b n b b be a number represented in s complement. Let K = k n k k represent B. We get K by applying the s complement operation to B. K is also called the s complement of B (and viceversa). Definition: The s complement of B is defined as K = ( n ) B, n = # of bits (including sign bit), where K = n i= k i i and B = n i= b i i. Note that K and B are treated as unsigned numbers in this formula. And ( n ) is the largest n-bit unsigned number. We can then show that the s complement operation amounts to inverting all the bits: n k i i = ( n ) b i i (k i + b i ) i i= n i= n i= = n k i + b i =, i k i = b i Example: Given B = = 9 in s complement, get the s complement representation of -9 using the formula: K = ( n ) B = ( 5 ) 9 = =. Recall that the formula treats K and B as unsigned integers. So, K = in unsigned representation, and K = 9 in s complement representation. It is much simpler to just invert each bit! With n bits, we can represent n numbers from n + to n. When using the 's complement representation, it is mandatory to specify how many bits we are using. Examples: +6= -6=, +5= -5=, +7= -7=. If -6=, we get +6 by applying the s complement operation to +6 = Get the s complement representation of 8: This is a positive number, thus the MSB=. The remaining n bits represent the magnitude. The magnitude is represented with a minimum number of 4 bits as 8=. Thus, using a minimum number of 5 bits, the number 8 in s complement representation is 8=. What is the decimal value of? We first apply the s complement operation to, which results in (+3). Thus =-3. What is the s complement representation of -4? We know that +4=. To get -4, we apply the s complement operation to, which results in. Thus =-4. 'S COMPLEMENT (C): Here, if the MSB=, the remaining n bits represent the magnitude. If the MSB=, the number is negative and the remaining n bits do not represent the magnitude. To invert the sign of a number in s complement representation, we apply the s complement operation to the number, which consists on inverting all the bits and add. Let B = b n b b be a number represented in s complement. Let K = k n k k represent B. We get K by applying the s complement operation to B. K is also called the s complement of B (and viceversa). Definition: The s complement of B is defined as K = ( n ) B +, n = # of bits (including sign bit), where K = n i= k i i and B = n i= b i i. Note that K and B are treated as unsigned numbers in this formula. We can see that we can first get the term ( n ) B by inverting all the bits; then we add to complete the equation. Example: Given = = 9 in s complement, get the s complement representation of -9 using the formula: K = ( n ) B + = ( 5 ) 9 + = 3 =. Recall that the formula treats K and B as unsigned integers. So, K = 3 in unsigned representation, and K = 9 in s complement representation. It is much simpler to just invert each bit (i.e., apply s complement operation) and then add! With n bits, we can represent n numbers from n to n. When using the 's complement representation, it is mandatory to specify how many bits we are using. 5 Instructor: Daniel Llamocca
Graphical interpretation of four-bit s complement numbers: If these 4-bit patterns represented unsigned integers, they would be numbers from to 5. As they represent s complement integers, then the numbers range from -8 to 7. 3 3 Examples: +6= -6=, +5= -5=, +7= - 7=. 4 If -6=, we get +6 by applying the s complement operation to +6 = Represent in s complement: This is a positive number, MSB=. The -5 5 remaining n bits represent the magnitude. We can get the magnitude 6 with a minimum 4 bits: =. Thus, using a minimum of 5 bits, the -6 9 7 number in s complement representation is =. -7 8 What is the decimal value of? We first apply the s complement -8 operation (or take the s complement) to, which results in (+3). Thus =-3. What is the s complement representation of -4? We know that +4=. To get -4, we apply the s complement operation to, which results in. Thus =-4. Getting the decimal value of a number in 's complement representation: If the number B is positive, then MSB=: b n =. n i= n i= n B = b i i = b n n + b i i = b i i (a) If the number B is negative, b n = (MSB=). If we take the s complement of B, we get K (which is a positive number). In s complement representation, K represents B. Using K = n B (K and B are treated as unsigned numbers): n k i i = n b i i We want a to express K in terms of b i, since the integer value K is the actual integer value of B. n i= K = k i i = b i i n = b n n + b i i n = n (b n ) + b i i i= n i= n n i= n B = K = n ( ) + b i i = n + b i i (b) Using (a) and (b), the formula for the decimal value of B (either positive or negative) is: i= n i= B = b n n + b i i Examples: = 4 + + = = 4 + 3 = 8 SUMMARY The following table summarizes the signed representations for a 4-bit number: n=4: SIGNED REPRESENTATION b 3b b b Sign-and-magnitude s complement s complement 3 4 5 6 7 - - -3-4 -5-6 -7 3 4 5 6 7-7 -6-5 -4-3 - - 3 4 5 6 7-8 -7-6 -5-4 -3 - - Range for n bits: [ ( n ), n ] [ ( n ), n ] [ n, n ] i= i= -3 n i= - 5-4 n i= 6 Instructor: Daniel Llamocca
C and C are representations of signed numbers. C and C represent both negative and positive numbers. Do not confuse the C and C representations with the C and C operations. Note that the sign-and-magnitude and the s complement representations have a redundant representation for zero. This is not the case in s complement, which can represent an extra number. In C, the number -8 can be represented with 4 bits: -8=. To obtain +8, we apply the C operation to, which results in. But cannot be a positive number. This means that we require 5 bits to represent +8=. SIGN EXTENSION UNSIGNED NUMBERS: Here, if we want to use more bits, we just append zeros to the left. Example: = with 4 bits. If we want to use 6 bits, then =. SIGNED NUMBERS: Sign-and-magnitude: The MSB only represents the sign. If we want to use more bits, we append zeros to the left. The leftmost bit is always the sign. Example: - = with 5 bits. If we want to use 7 bits, then - =. s complement (also applies to s complement): In many circumstances, we might want to represent numbers in 's complement with a fixed number of bits. For example, the following two numbers require a minimum of 5 bits: = 4 + + + = 9 = 3 + + + = +5 What if we wanted to use 8 bits to represent them? In s complement, we need to sign-extend: If the number is positive, we append 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 + + + = 9 = 3 + + + = +5 Demonstration of sign-extension in s complement: To increase the number of bits for representing a number, we append the MSB to the left as many times as needed: b n b n b b n b n b n b n b Examples: = = + = 5 = = 4 + + = 6 + 5 + 4 + + = We can think of the sign-extended number as an m-bit number, where m > n: b n b n b n b n b = b m b n b n b n b, where: b i = b n, i = n, n +,, m We need to demonstrate that b n b n b represents the same decimal number as b n b n b n b n b, i.e., that the sign-extension is correct for any m > n. We need that: b m b n b n b n b = b n b n b n b n b = b n b n b Using the formula for 's complement numbers: m m b m + i b i = n b n + i b i i= m i=n m n i= n i= m b m + i b i + i b i = n b n + i b i m b m + i b i = n b n m b n + b n Recall: r i l i=k i = n b n, i=n = rk r l+ r, r i l i=k n i= = k l+ = l+ k m i=n Then: m b n + b n ( m n ) = n b n m b n + m b n n b n = n b n n b n = n b n SIGNED NUMBERS ADDITION AND SUBTRACTION We will use the s complement representation for signed numbers. The advantage of the s complement representation is that the summation can be carried out using the same circuitry as that of the unsigned summation. Here the operands can either be positive or negative. ADDITION: We show addition examples of 4-bit signed numbers. Note that the carry out bit DOES NOT necessarily indicate overflow. In some cases, the carry out must be ignored, otherwise the result is incorrect. 7 Instructor: Daniel Llamocca
c 4 = c 3 = c = c = c = c 5 = c 4 = c 3 = c = c = c = c 5 = c 4 = c 3 = c = c = c = c 8 = c 7 = c 6 = c 5 = c 4 = c 3 = c = c = c = c 8 = c 7 = c 6 = c 5 = c 4 = c 3 = c = c = c = c 8 = c 7 = c 6 = c 5 = c 4 = c 3 = c = c = c = c 8 = c 7 = c 6 = c 5 = c 4 = c 3 = c = c = c = +5 = + + = -5 = + + = +5 = + - = -5 = + - = +7 = -3 = +3 = -7 = cout= cout= cout= cout= Now, we show addition examples of two 8-bit signed numbers. The carry out c 8 is not enough to determine overflow. Here, if c 8 c 7 there is overflow. If c 8=c 7, no overflow and we can ignore c 8. Thus, the overflow bit is equal to c 8 XOR c 7. Overflow: For 8-bit operations, this occurs when the summation falls outside the s complement range for 8 bits: [ 7, 7 ]. If there is no overflow, the carry out bit must not be part of the result. +9 = + +78 = +7 = overflow = c 8 c 7 = -> overflow! +7 [- 7, 7 -] -> overflow! -9 = + -78 = -7 = overflow = c 8 c 7 = -> overflow! -7 [- 7, 7 -] -> overflow! +9 = + -78 = +4 = overflow = c 8 c 7 = -> no overflow +4 [- 7, 7 -] -> no overflow -9 = + +78 = -4 = overflow = c 8 c 7 = -> no overflow -4 [- 7, 7 -] -> no overflow In general, for an n-bit number, overflow occurs when the summation falls outside the range [ n, n ]. The overflow bit can quickly be computed as overflow = c n c n. Also, c out = c n. To avoid overflow, a common technique is to sign-extend the two summands. For example, for two 4-bits summands, we add an extra bit; so, we use 5 bits to represent our numbers. In general, if the two summands are n-bits wide, the result will have at most n + bits. Recall that if there is no overflow in a summation result, the carry out bit must not be part of the result. +7 = + + = +9 = -7 = + - = -9 = Digital Circuit - Addition The figure depicts an n-bit adder for s complement numbers: x n- y n- x y x y x y overflow c out c n c n- c 3 c c c c in s n- SUBTRACTION Note that A B = A + C(B). To subtract two numbers represented in s complement arithmetic, we first apply the s complement operation to B (the subtrahend), and then add the numbers. So, in s complement arithmetic, subtraction is actually an addition of two numbers. The digital circuit for s complement subtraction is based on the adder. We account for the s complement operation for the subtrahend by inverting every bit in the subtrahend and by making the c in bit equal to. Here, we give up the borrow in. s s s 7-3 = 7 + (-3): +3= -3= cout = overflow = +7 = + -3 = +4 = 8 Instructor: Daniel Llamocca
x n- y n- x y x y x y overflow c out c n c n- c 3 c c c c in = s n- s s s Adder/Subtractor Unit for 's complement numbers: We can combine the adder and subtractor in a single circuit if we are willing to give up the input c in. x n- y n- x y x y x y add/sub add = sub = overflow c out c n c n- c 3 c c c s n- s s s add/sub y i f add/sub y i f If there is no carry in (or borrow in): The largest value (in magnitude) of addition of two n-bits operators is n + ( n ) = n. In the case of subtraction, the largest value (in magnitude) is n ( n ) = n +, or ( n ) ( n ) = n. Thus, the addition/subtraction of two n-bit operators needs at most n + bits. c n = c out is used in multi-precision addition/subtraction. SUMMARY Addition/Subtraction of two n-bit numbers (no carry in or borrow in): UNSIGNED SIGNED (C) Overflow bit c n c n c n Overflow occurs when: A + B [, n ], c n = (A ± B) [ n, n ], c n c n = Result range: [, n+ ] A + B [ n, n ], A B [ n +, n ] Result requires at most: n + bits c n = c out is used in multi-precision addition/subtraction for both signed and unsigned numbers. 9 Instructor: Daniel Llamocca
MULTIPLICATION OF INTEGER NUMBERS ARRAY MULTIPLIER FOR UNSIGNED NUMBERS A straightforward implementation of the multiplication operation is depicted in the figure: at every diagonal of the circuit, we add up all terms in a column of the multiplication. The figure shows the process and circuit for multiplying two unsigned numbers of 4 bits. x cin y a 3 a a a x b 3 b b b FULL ADDER cout s a 3 b a b a b a b a 3 b a b a b a b a 3 b a b a b a b a 3 b 3 a b 3 a b 3 a b 3 p 7 p 6 p 5 p 4 p 3 p p p b(3) b() b() b() a() a b 3 a b a b a b m 3 m m m s 3 s s s a() c c c a b 3 a b a b a b m 3 m m m p() s 3 s s c c c s a() a b 3 a b a b a b m 3 m m m p() s 3 s s c c c s a(3) a 3 b 3 a 3 b a 3 b a 3 b m 33 m 3 m 3 m 3 p() s 33 s 3 s 3 c 3 c 3 c 3 s 3 m 43 m 4 m 4 m 4 p(3) p(7) p(6) p(5) p(4) Instructor: Daniel Llamocca
MULTIPLICATION OF SIGNED NUMBERS A straightforward implementation consists of checking the sign of the multiplicand and multiplier. If one or both are negative, we change the sign by applying the s complement operation. This way, we are left with unsigned multiplication. As for the final output: if only one of the inputs was negative, then we modify the sign of the output. Otherwise, the result of the unsigned multiplication is the final output. x x x x x x x Note: If one of the inputs is n, then the s complement of it is n, which requires n + bits. Here, we are allowed to use only n bits; in other words, we do not have to change its sign. This will not affect the final result since if we were to use n + bits for n, the MSB=, which implies that the last row is full of zeros. x x x x x x Final output: It requires n bits. Note that it is only because of the multiplication of n by n that we require those n bits (in s complement representation) Instructor: Daniel Llamocca
BINARY CODES We know that with n bits, we can represent n numbers, from to n. This is a commonly used range. However, with n bits, we can also represent n numbers in any range. Moreover, with n bits we can represent n different symbols. For example, in 4-bit color, each color is represented by 4 bits, providing 4 distinct colors. Each color is said to have a binary code. N = 5 symbols. With bits, only 4 symbols can be represented. With 3 bits, 8 symbols can be represented. Thus, the number of bits required is n = 3 = log 5 = log 8. Note that 8 is the power of closest to N=5 that is greater than or equal to 5. In general, if we have N symbols to represent, the number of bits required is given by log N. For example: Minimum number of bits to represent 7, colors: Number of bits: log 7 = 7 bits. Minimum number of bits to represent numbers between 5, and 9,96: There are 9,96-5,+=497. Then, number of bits: log 497 = 3 bits. 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 7 = 8. Each character is said to have a binary code. Unicode: This code can represent more than, characters and attempts to cover all world s scripts. A common character encoding is UTF-6, which uses pair of 6-bit units: For most purposes, a 6 bit unit suffices ( 6 = 65536 characters): (Greek theta symbol) = 3D (Greek capital letter Omega): 3A9 Ж (Cyrillic capital letter zhe): 46 Instructor: Daniel Llamocca
BCD Code: In this coding scheme, decimal numbers are represented in binary form by independently encoding each decimal digit in binary form. Each digit requires 4 bits. Note that only values from are 9 are represented here. This is a very useful code for input devices (e.g.: keypad). But it is not a coding scheme suitable for arithmetic operations. Also, notice that the binary numbers () to (5) are not used. Only out of 6 values are used to encode each decimal digit. Examples: Decimal number 47: This decimal number can be represented as a binary number:. In BCD format, this would be: Decimal number 58: This decimal number can be represented as a binary number:. In BCD format, the binary representation would be: The BCD code is not the same as the binary number! BCD decimal # 3 4 5 6 7 8 9 There exist many other binary codes (e.g., reflective gray code, 6-3-- code, -out-of-5 code) to represent decimal numbers. Usually, each of them is tailored to a specific application. REFLECTIVE GRAY CODE: Decimal g g Number b b b g g g g 3 g g g b n- b n- b b 3 4 5 6 7 g n- g n- g g g n- g n- g g b n- b n- b b 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, otherwise. The RGC encoding makes that between areas only one bit changes, thereby reducing the possibility of an incorrect reading (especially when the beam between adjacent areas). For example: from to only one bit flips. If we used to, two bits would flip: that would be prone to more errors, especially when the beams are close to the line where the two areas meet. 35 57.5 8.5.5 5 9 67.5 47.5 7 45 9.5.5 337.5 35 EMITTER RECEPTOR g g g g 3 Angle -.5.5-45 45-67.5 67.5-9 9 -.5.5-35 35-57.5 57.5-8 8 -.5.5-5 5-47.5 47.5-7 7-9.5 9.5-35 35-337.5 337.5-36 g 3 g g g 3 Instructor: Daniel Llamocca
INTRODUCTION TO FIXED-POINT ARITHMETIC 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: Formula to convert it to decimal: n (b n b n b b. b b b k ) D = b i i = b n n + b n n + + b + b + b + b + b k k i= k Conversion from binary to hexadecimal (or octal): (unsigned numbers) Binary:.. octal: 5. 6 Binary:.. Example: (unsigned number). = 3 + + + + + + 3 =.65 Example: 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, the resulting integer parts resulting is the result. base.65 MSB.65x =.5 = +.5 hexadecimal: base???? 5 base.7. A 8 MSB.7x =.4 = +.4 base????.5x =.5 = +.5.5x = = +.4x =.8 = +.8.8x =.6 = +.6. stop here!.6x =. = +..x =.4 = +.4.4x =.8 = +.8. Example (signed number): Convert -379.875 to the s complement representation. First, we get the binary representation of +379.875, and then apply the s complement operation to that result. 379 =. In s complement: 379 =,.875 =.. Then: 379.875 =.. This is the s complement representation of 379.875. Finally, we get -379.85 by getting the s complement of the previous result: -379.875 =. = xe84.c8 (to convert to hexadecimal, we append zeros to the LSB and sign-extend the MSB) 4 Instructor: Daniel Llamocca