Lecture 2 Binary and Hexadecimal Numbers Purpose: Review binary and hexadecimal number representations Convert directly from one base to another base Review addition and subtraction in binary representations Determine overflow in unsigned and signed binary addition and subtraction Lecture 2 Page 1
The Need for Other Bases Humans are used to the decimal number system, also called radix-10 or base-10. To state the obvious, base-10 means that a digit has one of ten possible values, 0 through 9. In computers, numbers are stored in binary, also called radix-2 or base-2, using arrays of flip-flops. Each digit may take one of two values, either 0 or 1. Long strings of these 1 s and 0 s are cumbersome to use, so we will usually represent binary numbers using hexadecimal, also called radix-16 or base-16. It is important to note that no computer actually stores values in hardware using hexadecimal. This number system is only a convenience for humans. All of these number systems are positional. Lecture 2 Page 2
Unsigned Decimal Numbers are represented using the digits 0, 1, 2,, 9. Multi-digit numbers are interpreted as in the following example: 793 10 = Unsigned Binary Numbers are represented using the digits 0 and 1. Multi-digit numbers are interpreted as in the following example: 10111 2 = In binary, each digit is called a bit. Since we use binary to represent the values stored in a group of flip-flops, we usually specify a binary system by the number of bits (flip-flops) being used to store each number. When we write numbers in this system, we will write all bits, including leading 0 s. The number above is expressed in 5-bit binary. The number below is in 8-bit binary. 00010111 2 Lecture 2 Page 3
Unsigned Hexadecimal Numbers are represented in hexadecimal using the digits 0, 1, 2,, 9, A, B,, F where the letters represent values: A=10, B=11, and so on to F=15. Note that this gives sixteen possible values for each digit. Multidigit numbers are interpreted as in the following example: 76CA 16 = Notes on Bases Since all three number bases will be used, including the correct subscript when a number is written out of context is mandatory. Pronunciation Words like ten, twenty, and one-thousand refer to specific numbers of items, regardless of how the numbers are written. To avoid confusion, binary and hexadecimal numbers are spoken by naming the digits followed by binary or hexadecimal. For example, 1000 16 is pronounced one zero zero zero hexadecimal. One-thousand is actually 3E8 16. Lecture 2 Page 4
Ranges of Unsigned Number Systems System Lowest Highest Number of Values 4-bit binary (1-digit hex) 8-bit binary (1 byte) (2-digit hex) 16-bit binary (2 bytes) (4-digit hex) n-bit binary Lecture 2 Page 5
2 s Complement Binary Numbers Most microprocessors today use 2 s Complement numbers to represent systems with positive and negative values. Hardware performs addition and subtraction on binary values the same way whether they represent unsigned systems or 2 s complement systems, and this greatly simplifies the design of the processor The only difference between unsigned binary systems and 2 s comp. binary systems is that the most significant bit in signed systems has a weight of -2 n-1. Both systems are also defined by the number of bits being used, and as with unsigned, we must write down all bits. To distinguish between the two, we will use a 2c subscript to indicate a 2 s comp. number. Example: Convert 10011010 2c to decimal. Example: Convert 11011 2c to decimal. Example: Convert 01011 2c to decimal. It is very important to note that microprocessors usually view a group of bits as simply a group of bits. It is the human that interprets the group as an unsigned value, signed value, or also as just a group of bits. Lecture 2 Page 6
Ranges of Signed Number Systems System Least Greatest Number of Values 4-bit binary 8-bit binary 16-bit binary n-bit binary Note that the least representable value has a single 1 in the column with a negative weight and 0 s in the columns with positive weights. The greatest representable number is just the opposite: 1 s in the positively weighted columns and a 0 in the column with a negative weight. Lecture 2 Page 7
Sign Bit Since the leftmost column has a negative weight, and the magnitude of that weight is larger than the weights of all the positive columns added together, any number with a 1 in the leftmost column will be negative. If the leftmost bit has a 0, then there is no negative contribution, and the value will be positive. Hence, the sign of the number can be determined by inspection. Negating a 2 s Complement Number The official way to negate a value is to subtract it from zero. This will generate a number with the same magnitude but with the opposite sign. The second method is to perform the 2 s complement, which is the following two steps: 1. Perform the 1 s complement (flip all the bits) 2. Add 1 Example: Negate 00101001 2c (41 10 ) Lecture 2 Page 8
Converting Between Number Systems Given the three number systems (binary, hexadecimal, and decimal), there are six possible conversions to allow us to convert directly from one to another. Binary to Decimal: This was covered earlier in this lecture by determining the weights for each column and adding them up, either as unsigned or as signed. Hexadecimal to Decimal: This was also covered earlier in this lecture by determining the weights for each column and adding them up. Decimal to Binary (or Hexadecimal): This conversion is more of a process than the others. 1. Successively divide the decimal number by the new base and keep track of the remainders generated. 2. Stop dividing once the quotient reaches 0. 3. Write the remainders in the opposite order than they were generated. 4. Add leading digits if necessary. Lecture 2 Page 9
Example: Convert 53 10 to 8-bit unsigned binary. Example: Convert 172 10 to 2-digit hexadecimal. Lecture 2 Page 10
Note: To convert a negative value, first convert the magnitude to the correct number of bits as done above. Then, negate the result to get the final answer. Binary to Hexadecimal: This conversion is the reason that hexadecimal is used in the first place. Since once hexadecimal digit can represent 16 different values, and four bits can also represent 16 (2 4 ) different values, the bits of the binary number will be grouped together in 4 s and replaced by the hexadecimal digit with the same value. Example: Convert the binary numbers below to hexadecimal. 100101011110 2 011010101011 2c Note that a binary number may not always contain an integer multiple of 4 bits. In these cases, always extend the binary number by padding it with zeros, whether the number is unsigned or signed. Example: Convert the binary numbers below to hexadecimal. 101011110 2c 11011 2c Lecture 2 Page 11
Hexadecimal to Binary: As with binary to hexadecimal, this conversion is by inspection. Each hexadecimal digit is replaced with the four bits that represents the same value. Example: Convert the following hexadecimal numbers to binary. BEFA 16 73FC 16 Hexadecimal is not usually interpreted as signed or unsigned. It is simply a more convenient method for humans to discuss binary patterns. By default, we will assume that the binary pattern has four bits for every hexadecimal digits used, as done above. However, we can also specify a binary system with any number of bits, as done on the latter half of the previous slide. We must explicitly state the binary system being used, and we will ignore the padding 0 s. Example: Convert the following hexadecimal numbers to the specified binary system. 07B 16 to 9-bit signed 1F 16 to 5-bit unsigned Lecture 2 Page 12
Binary Arithmetic Unsigned and signed addition and subtraction generate the same numerical result. The difference is determining if overflow or underflow (usually grouped together generically as overflow ) occurs. The methods below work for both addition and subtraction. For unsigned: For signed: The above methods are easily implemented in hardware. For humans, there is an alternate approach for signed arithmetic. Addition: If the two numbers being added have the same sign, the answer must have that sign. Otherwise, overflow occurs. Signed addition cannot generate overflow if the two numbers being added have different signs. Subtraction: Lecture 2 Page 13
Example: Determine the result, if unsigned overflow occurred, and if signed overflow occurred. (Note that the binary numbers are shown with an unsigned subscript for simplicity.) 0 1 1 0 1 0 1 1 2 + 0 1 0 1 1 0 1 0 2 0 1 1 0 0 1 0 1 2-1 1 0 1 1 0 1 1 2 Lecture 2 Page 14
Modular Number Systems 1111 0000 0001 1110 0010 Wrap-around point for unsigned 1101 0011 1100 0100 1011 Wrap-around point for signed 0101 1010 0110 1001 0111 1000 Here s another way to visualize overflow. Binary systems are modular. For our purposes, modular means that a constant number of digits are used. Above, increasing a value (i.e. add a positive or subtract a negative) moves us clockwise around the number line. Decreasing a value (i.e. subtract a positive or add a negative) moves us counterclockwise. If the wrap-around point is crossed during this move, overflow occurs. Lecture 2 Page 15
Extending Binary Numbers When performing arithmetic operations, the binary numbers must have the same number of bits. Therefore, it is sometimes necessary to extend the shorter number so that it has the same number of bits as the longer number. This must be done in a manner such that the new, longer number still represents the same value as the shorter number. For unsigned: For signed: Example: Extend the binary numbers below to 16 bits. 01101111 2 100101101 2 01110 2c 10011001 2c Note that a binary number can always be extended correctly. Lecture 2 Page 16
Truncating Binary Numbers There are times when a value will be expressed in more bits than needed, and the number of bits being used should be reduced to save space. This is done by discarding the most significant bits of a binary number. Technically, truncation may always be performed. However, we will say that it is not possible if it yields a shorter number that does not represent the same value as the original, longer number. For example, of the 65536 possible 16-bit numbers, only 512 may be correctly truncated to the 512 possible 9-bit numbers. Unsigned: All bits discarded must be 0 s, otherwise the shorter number will not be accurate. Signed: All bits discarded must be the same as the new sign bit of the shorter number, otherwise the shorter number will not be accurate. Example: Determine which of the 16-bit values below can be truncated to 8 bits in the specified system. 0000 0000 1011 0111 2 1111 1111 1011 0111 2 0000 0000 1011 0111 2c 0000 0000 0011 0111 2c 1111 1110 1011 0111 2c 1111 1111 1011 0111 2c Lecture 2 Page 17