|
|
|
|
Internal Representation of Numbers: HexadecimalWe have looked at the decimal and binary number systems. Now we are ready for another, base 16 or hexadecimal (hex for short). The hexadecimal number system is commonly used to represent computer data, because it allows for a shorthand method of representing binary data. Since a byte is commonly the smallest unit of memory addressed, and one byte consists of 8 bits, we often express binary data by symbols representing groupings of four bits (Binary Digits). (Therefore, one byte can be represented by two hexadecimal symbols). The hexadecimal number system allows one to express the 16 possible distinct values of those four bits with a corresponding symbol. The end result is that, instead of printing 8 symbols, representing the eight binary digits in a byte, we can represent the contents of a byte using only two symbols. This allows for more compact representation of internal data. As well, if we need to determine the binary data represented by a hexadecimal number, we only have to convert each of the two hexadecimal symbols back to their binary equivalent. Lets do our 137 decimal conversion again, only this time in Hexadecimal. Remember that 137 10 represents 1 unit of 10 raised to the second power, 3 units of 10 raised to the first power, and 7 units of 10 raised to the zeroth power. 1x102 + 3x101 + 7x100 = 137 , or 1x100 + 3x10 + 7x1 = 137 Hexadecimal Number System
Hex to Decimal ConversionHow would we convert 137 to hex? Start with a lookup table: Conversion between Binary and HexadecimalAs mentioned, the main reason for using the hexadecimal number system is as a shorthand method to represent binary data. Therefore, the conversion between binary and hexadecimal numbers is relatively straightforward.Binary to Hexadecimal
Therefore, 10101100000011 2 can be converted by
Hexadecimal to Binary
Binary Addition and Negative NumbersAt this point we have covered how the computer represents POSITIVE integers. Now we'll look at Negative numbers. This is harder, mainly because of the representation of zero. Signed MagnitudeSigned magnitude uses the most significant bit in the representation of a number and uses that to be the sign of the number. For example: 137 decimal is represented 0000 0000 1000 1001 in a 16 bit word. A signed magnitude system would represent -137 as 1000 0000 1000 1001. This system has the problem that there are 2 representations for zero: +0 and -0. Ambiguity is a bad thing with computers. Also, (a - b) and (a + (-b)) are not the same. A computer that uses signed magnitude representation of integers must add extra circuitry to deal with these problems. One's ComplementOne's complement is just the positive number with all the bits reversed. (137)10 = 0000 0000 1000 1001 would be 1111 1111 0111 0110. This was used in the Univac 1100 series (1962) but has a similar problem to signed magnitude: that there is two representations for zero: 0000 0000 0000 0000 and 1111 1111 1111 1111. Both are valid bit patterns. Two's ComplementA two's complement is formed by adding one to a One's Complement. This has the properties of every bit pattern is a valid number and every number is unique. This system is used by most, if not all, computers today. So, let's look at binary math with Two's Complement numbers. Binary AdditionIdentical in method to decimal you just have to remember there is only 2 digits - 0 and 1. 0 + 1 = 1 and 1 + 1 = 10 (0 and carry the 1 into the next position) 0 01 10 11 +1 +1 +1 +1 _____ ____ ____ ____ 1 10 11 100 To convert a positive number to a negative number in two's complement do a one's complement and add 1. For example:
0000 0000 0000 0001 Examples:
Give the internal representation of the following numbers.
Binary Addition Using Two's ComplementOne of the nice features of using two's complement representation is the ability to perform mathematical operation involving integers in terms of binary additions only. 109 - 59 can be represented as 109 + (-59). When we have the computer evaluate this expression, internally the CPU is actually performing a binary addition by first converting the negative operand to its Two's Complement representation and then performing an addition. Continuing with our example,
You try 59 - 109 ? |
|
Last updated: September 11, 2003. |