1 Decimal to Binary
Total Page:16
File Type:pdf, Size:1020Kb
CS 2160: Computer Organization and Assembly Language Programming Change of Base; Negative Numbers See also: section 2.4 of text Part I: Change of Base Numeric bases are a crucial topic in Computer Science. Humans are very accustomed to counting by tens; after all, we have ten fingers that are very convenient for counting. However, electrical circuits don't have ten levels of \on-ness." In fact, they only have two states: on and off. This is an oversimplification, of course, but the simple distinction between a high-voltage and a low voltage state is both easy to represent in a computer and easy to design circuits around. If the voltage is high, do X. Otherwise, do Y. This didn't stop early computer designers from trying to represent numbers in programs and computer memory in base-10 (decimal), but they very quickly realized that this was clunky. However, referring to numbers to be used in computers in terms of binary strings is also very cumbersome. Hexadecimal (base-16) and octal (base-8) are convenient ways to describe numbers such that they can be very easily converted to and from binary while still being memorable (some that spring to mind from the hexadecimal realm are 0xDEADBEEF and 0xCAFEBABE). 1 Decimal to binary Converting from decimal to binary is a good starting point. Start by writing down the decimal number in the left of two columns. For instance: 137 If the number is odd, write a 1 in the right column. If it is even, write a 0. Divide the number by 2, dropping the remainder if the number is odd, and write the quotient under the original number. 137 1 68 Repeat this process until you write a 0 in the left column. 137 1 68 0 34 0 17 1 8 0 4 0 2 0 1 1 0 Starting at the bottom, write the digits in the right-hand column in order to get the binary equivalent. In the example, this is 10001001. Often, to indicate that this is a representation in binary, a subscript 2 can be used: 100010012 (the book uses 10001001two to indicate the same thing). 1 2 Binary to decimal To convert back, the process is essentially the reverse. Starting with the left-most (most significant) bit, add each consecutive bit to a mental accumulator, multiplying the accumulator contents by two (2) each time. Using the previous example, this process would look like the following: 0 × 2 = 0 + 1 = 1 1 × 2 = 2 + 0 = 2 2 × 2 = 4 + 0 = 4 4 × 2 = 8 + 0 = 8 8 × 2 = 16 + 1 = 17 17 × 2 = 34 + 0 = 34 34 × 2 = 68 + 0 = 68 68 × 2 = 136 + 1 = 137 3 Binary to octal Octal (base-8) uses the digits 0 through 7 to represent numbers. This is convenient, coming from binary, as the numbers 0 through 7 can be represented with exactly three binary digits, as the table shows: Octal Binary 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111 Using this table, we can easily convert a binary number into groups of three digits and derive the correct octal digits: 010 001 001 2 1 1 Since 211 looks more like a valid decimal number than the binary example earlier, we explicitly note the base: 2118. In addition, octal numbers are often written with a leading zero, such as 0211. 4 Binary to hexadecimal Hexadecimal (base-16) follows a similar pattern to octal, using the letters A through F to represent digits with values of 1010 through 1510. The table below shows binary equivalents to the hexadecimal digits. Hex Binary Hex Binary 0 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111 The hexadecimal equivalent to 13710 is 8916 determined using a similar grouping scheme to the octal conversion. Hexadecimal is often written with a leading 0x: 0x89. 2 5 Other conversions Converting hexadecimal and octal to binary is simply a matter of expanding each digit to the corresponding binary representation and concatenating them together. While you could convert directly from decimal to hexadecimal and octal and vice-versa using the same quotient-remainder and multiply-by-base-and-add-next-digit strategies as are used for binary, this is a cumbersome and tedious process, so binary serves as a convenient gateway between bases. Part II: Representing negative numbers 6 Sign-magnitude For an n-bit number, n − 1 bits are used to represent the maginitude of the number with the first bit being used to represent the sign, with 0 representing positive and 1 representing negative. For instance, a four-bit representation of −2 would be 1010. However, sign-magnitude numbers are difficult to add and subtract. It can't be done directly (1010 − 1001 = 0001 or 1, which is incorrect), requiring extra logic to make such an addition or subtraction work. 7 One's complement One's complement solves the issue of direct subtraction by using the complement of magnitude to represent its negative (−2 would be 1101). This represents the exact same range of numbers as sign-magnitude (1101 − 1110 = 11111, which results in an \end-around borrow" that must be subtracted out of the result, leading to 1110, the proper one's-complement representation of −1), but leads to another problem. In one's complement, zero is represented by 0000, however, it is also possible to create a negative zero by simply taking the complement: 1111. This is an issue for both mathematicians and computers. 8 Two's complement Two's complement solves this problem by using a two-step negation. First, complement all the bits, then add 1. It is trivial to see that by doing this to a binary representation of zero in an attempt to get negative zero, the result is identical to that of \positive" zero. In addition, direct addition and subtraction still works. Taking the previous example of −2 − −1 = −1, the two's complement equivalent is 1110 − 1111 = 1111 (no \end-around borrow" necessary). Likewise, 1110 + 1111 = 1101, which is the two's complement representation of -3. 3.