Binary-Decimal Conversions
Conversions from binary to decimal and vice-versa are based on the calculations shown above. For converting binary to decimal, powers of two are summed, where a “1” appears in the binary number, such as was shown above for the conversion 101112 = 2310. A simple approach to convert decimal to binary is illustrated with an example. Repeatedly divide by 2, and record the remainder until the remainder is zero. The remainders are recorded from right to left.
For example, for converting 23 to binary (the converse of the example above) yields the following calculations:
23/2 = 11 with a remainder of 1. Since the remainder is 1, record binary number 1.
11/2 = 5 with a remainder of 1. Record the binary number = 11.
5/2 = 2 with a remainder of 1. Record the binary number = 111.
2/2 = 1 with a remainder of 0. Record the binary number = 0111.
1/2 = 0 with a remainder of 1. Record the binary number 10111.
Therefore, 2310 = 101112, as expected.
Computers use fixed-length binary numbers to represent integers. For example, 2 bits can represent 22 = 4 different values, 00, 01, 10, and 11, or (0, 1, 2, and 3 in decimal). With 4 bits, 24 = 16 values, from 0 to 15, can be represented:
00002 = 010
00012 = 110
00102 = 210
00112 = 310
01002 = 410
01012 = 510
01102 = 610
01112 = 710
10002 = 810
10012 = 910
10102 = 1010
10112 = 1110
11002 = 1210
11012 = 1310
11102 = 1410
11112 = 1510
Numbers represented by 8-bits are very common in digital computer systems. Characters, for example, are indicated by 8-bits. There are 28 = 256 different 8-bit numbers, ranging from 0 to 255.
Binary arithmetic is arithmetic based on binary numbers and operating on bits. The basic binary addition operations are: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10. The “1”, or most significant digit in the sum 1 + 1 = 10, is known as the carry bit. When adding larger binary numbers, binary arithmetic is performed from right to left, as in decimal addition, and the carry bits are taken into account. The concept will be illustrated with examples of adding 4-bit binary numbers.
Example 1:
1010
+ 0100
1110
In decimal arithmetic, 10102 = 1010, 01002 = 410, and 10102 + 01002 = 11102 = 1410.
Example 2:
1010
+ 1100
10110
The underlined “1” is the carry bit. In decimal arithmetic, 10102 = 1010, 11002 = 1210, and 10102 + 11002 = 101102 = 2210.
Example 3:
11
1010
+ 0110
10000
The underlined “1”s denote the carry bits. In decimal arithmetic, 10102 = 1010, 01102 = 610, and 10102 + 01102 = 100002 = 1610.
Example 4:
111
1111
+ 1011
11010
Recall that 1 + 1 + 1 = 11 in binary. The underlined “1”s denote the carry bits. In decimal arithmetic, 11112 = 1510, 10112 = 1110, and 11112 + 01102 = 110102 = 2610.
A final, more complex example performs binary addition on 8-bit numbers. For clarity, groups of 4-bits are written as being separated.
Example 5:
1111 111
1011 1111
+ 1100 0111
1 1000 0110
In decimal arithmetic, 1011 11112 = 19110, 1100 01112 = 19910, and 1011 11112 + 1100 01112 = 39010, as 1 1100 01102 = (1 x 28) + (1 x 27) + (0 x 26) + (0 x 25) + (0 x 24) + (0 x 23) + (1 x 22) + (1 x 21) + (0 x 20) = 256 + 128 + 0 + 0 + 0 + 0 + 4 + 2 + 0 = 39010.
Arithmetic overflow occurs when the computer represents a number that is too large to fit into a specified number of bits. For instance, 1410 = 11102 and 210 = 00102 are both represented with 4 bits, but their sum, 14 + 2 = 16 = 100002 requires 5 bits. In Example 5 above, if numbers are to be represented with 8 bits, the sum of the two 8-bit numbers result in an arithmetic overflow, as 9 bits are required to store 1 1000 01102.
Signed integers include negative numbers. In sign/magnitude notation, the most significant (leftmost) bit indicates the sign, and the remaining bits indicate the value. A “1” in the most significant bit indicates a negative number. For example:
+7 = 0111, -7 = 1111
However, zero can then be represented in two ways, leading to confusion and errors: with 4 bits, 0 = 0000 and 1000. To alleviate this problem, two’s complement representation is used. The procedure is as follows. To obtain the negative of a number, flip (change the parity ofs) every bit (that is, all zeros become ones, and all ones become zeros), and add 1. For example: +7 = 0111, -7 = 1000 + 1 = 1001, 0 = 0000, and -0 = 1111 + 1 = 0000.
The two’s complement allows 8-bit values to range from -128 to 127. Thus, there are still 256 integers that can be represented.
As another example, to represent -104, the two’s complement is calculated as follows:
10410 = 64 + 32 + 8 = 26 + 25 + 23 = 0110 10002 in 8-bits.
Therefore, -104 = 1001 0111 + 1 = 1001 1000.
As a final example, recall that short integers, integers represented with 16 bits, range in value from -32,768 to 32,767. To see this, it is remembered that the most significant bit (MSB), or the leftmost bit, is the sign bit, where 0 indicates a positive value, and 1 indicates a negative value. The largest positive integer in this format is therefore 0111 1111 1111 11112. The decimal equivalent can either be computed by adding powers of 2, or by the following Python operation:
>>> int('0111111111111111', 2)
32767
The smallest value (or largest negative number) that can be represented is given with the two’s complement. Therefore, this two’s complement representation is converted to decimal by reversing the steps in converting from decimal to two’s complement. First, subtract 1 from the binary number, change the polarity (i.e., flip) each of the bits, convert to decimal, and make that number negative. In Python, there are many valid approaches to perform this task, one of which is given below.
>>> ## Convert FROM two's complement TO decimal by reversing the process of
converting FROM decimal TO two's complement:
>>> ## (1) Subtract 1.
>>> ## (2) Flip every bit.
>>> ##
>>> ## First, represent the largest negative number in two's complement
notation.
>>> b = '1000000000000000'
>>> ##
>>> ## Subtract 1, after first converting to decimal for simplicity.
>>> bCompStr = bin(int(b, 2) - 1)
>>> ## Subtract 1, after first converting to decimal for simplicity. Use
Python formatting to remove the '0b' (for binary) prefix, and pad zeros
to 8 bits.
>>> bCompStr = "{0:016b}".format(int(b, 2) - 1)
>>> bCompStr
'0111111111111111'
>>> ## Convert the string to a list, flipping each bit.
>>> bList = [1 - int(a) for a in bCompStr]
>>> bList
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> bSmallest = ''.join([str(a) for a in bList])
>>> bSmallest
'1000000000000000'
>>> ## Finally, get the smallest (most negative) short integer in decimal, and make it negative.
>>> -int(bSmallest, 2)
-32768
As a check, the two’s complement representation of the number -32,768 will now be calculated. First, note that 32,768 = 215, and therefore 3276810 = 1000 0000 0000 00002. The two’s complement is 0111 1111 1111 11112 + 0000 0000 0000 00012 = 1000 0000 0000 0000, which is the result with which the previous example started.
Therefore, signed short integers represented with 16 bits are in the range from -32,768 to 32,767.
For unsigned short integers, the most significant bit does not indicate the sign, and therefore, all 16 bits are used. The minimum value is 0000 0000 0000 00002 = 010, and the maximum value is 1111 1111 1111 11112 = 65,53510.