3Note, ‘Kilobyte’ is denoted by KB (and Kilobit is denoted by Kb). The K, M, and G identify only the prefixes, since we may count other things besides bytes.
Binary Numbers
In computer science we deal almost exclusively with binary numbers. it will be very helpful to memorize some binary constants and their decimal and English equivalents.
By English equivalents we mean expressions such as ‘10 Megabytes’, ‘ 45 Kilobytes’, and ‘200Gigahertz’ (a byte is eight binary digits treated as a group to represent a single quantity. A byte of storage is typically required to store a single alphanumeric
character.)
20=1 =1 25=32 =100000 21=2 =10 26=64 =1000000 22=4 =100 27=128 =10000000 23=8 =1000 28=256 =100000000 24=16 =10000 29=512 =1000000000
Large quantities are generally given in powers of two, but also have English names commonly associated with powers of ten. For instance, ‘1 Kilobyte’ is not 1000 (103) bytes, but is 210 (1024) bytes and is referred to as a ‘kilobyte’, or 1KB. In computer science, the phrase ‘one thousand’ will usually mean 102410, not 100010. Similarly, a
‘million’ is 220 and a billion is 230; 220 bytes is called a megabyte (1 MB), and 230 bytes is a ‘gigabyte (1 GB).
. 210=1024=1K3
220=1M 230=1G
To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:
2a+b = 2a x 2b 2a-b = 2a/2b 2-a = 1/2a Examples:
12. Converting binary exponential expressions to 'English' abbreviation
212 = 22 x 210 = 4 x 1K = 4K
233 = 23 x 230 = 8 x 1G = 8G 227 = 27 x 220 = 128 x 1M = 128M
13. Converting 'English' abbreviations to binary exponential expressions.
32G = 32 x 1G = 25 x 230 = 235 16K = 16 x 1K = 24 x 210 = 214 512M = 512 x 1M = 29 x 220 = 229
Practice Problems - Binary-to-English Conversions
1. Convert the following binary exponential expressions to their 'English' counterparts:
23 213 223 233 227 215 231 220 238
2. Convert the following 'English' expressions to their binary exponential equivalents:
1K 256 16M 32K 128G 2M 4G 512K 8M 3. Word problems
a. How much memory is supported by a system with 24-bit memory addresses?
b. How large an address is needed to address a memory of 4GB?
c. How many op code bits are need in an instruction which supports 128 different operations?
d. How many registers are supported by an instruction with a register ID field of 6 bits?
Binary Number Conversion
We will frequently have occasion to convert binary numbers to decimal numbers. The techniques previously described for number systems in general are fully applicable here. For example, converting binary to decimal:
10110112 = 1x26 + 0x25 + 1x24 + 1x23 + 0x22 + 1x21 + 1x20
= 64+0+16+8+0+2+1
= 9110
.1012 =1x2-1 + 0x2-2 + 1x2-3
= 1/21 + 0/22 + 1/23
= 1/2 + 0/4 + 1/8 = (4+1)/8 = 5/8
= .62510
For the interested student, note that there are ways to speed up the conversion of integer binary numbers to decimal. They rely on two things. First that you memorized the decimal/binary equivalents of the numbers 0 thru 15, as shown in table TN1;
second, remember that shifting a binary number one position to the left (inserting a zero in the rightmost postion) multiplies that number by 2 (just as shifting a decimal number to the left multiplies it by 10). Then 1011011 could be rapidly converted as follows:
10110112 = 1011000 + 011
= 11 x 8 + 3 since the first 4 digits (1011 = 11) are shifted 3 places (=8) = 9110
or, equivalently, 10110112 = 1010000 + 1011 = 10 x 8 + 11 = 9110 Examples of converting decimal to binary:
9110 to binary -
91/2 = 45 with a remainder of 1 45/2 = 22 with a remainder of 1 22/2 = 11 with a remainder of 0 11/2 = 5 with a remainder of 1 5/2 = 2 with a remainder of 1 2/2 = 1 with a remainder of 0 1/2 = 0 with a remainder of 1 giving 10110112
.7510 to binary -
.75 x 2 = 1.50 first digit is 1 .50 x 2 = 1.00 second digit is 1 .00 x 2 = 0 we are done giving .112
Alternatively, some students find it faster to use the following scheme for converting decimal integer numbers to binary. Simply find the largest power of 2 which is smaller than or equal to the given number (you should have memorized the first 10 powers of 2), and subtract this power of two from the number. Continue subtracting powers of two from the result of the previous subtraction until the result is zero. The powers of 2 which were able to be subtracted are then the positions of all the 1's in the result.
Example, 9110
91 - 64 = 27 Therefore 64 = 26 is the position of the first 1 27 - 16 = 11 Therefore 16 = 24 is the position of the second 1 11 - 8 = 3 8 = 23 is the third position
3 - 2 = 1 2 = 21 is the fourth position 1 - 1 = 0 1 =20 is the fifth 1 in the answer giving 10110112
Negative Binary Numbers
In what follows, keep in mind that, since we are interested in using these numbers within the hardware of a computer, there is a finite amount (usually small) of space (storage) allocated for each number. In the computer’s memory this may be referred to as a ‘word’. In the CPU such data is held in storage devices called ‘registers’. Each register has a finite size, measured in ’bits’, where each bit is a binary digit (in fact, the word bit is a contraction of the words ‘binary digit’. We assume that every binary
number uses all the bits available in the specified register. A group of adjacent 8 bits in a register is called a ‘byte’. A 32-bit register, therefore, is also a 4-byte register.
There are six common ways of representing a number in binary:
Unsigned
Sign and Magnitude 2's complement Biased
Binary Coded Decimal Floating Point
The second, third and fourth are binary representations which allow for the representation of negative as well as positive numbers.
5. Unsigned binary numbers. All the bits in the register are weighted per there bit positions, and are numbered 0 through n-1 from right to left . If the register is n
bits wide, it can hold 2n different values, in the range (assuming integers only) of 0 through +2n - 1. For example, a 16-bit register holds 216 = 65536 values, 0 thru 65545. Note that all arithmetic results which end up in an n-bit register can never have a value greater than 2n - 1, and is thus effectively arithmetic modulo 2n. Keep this in mind for future discussion.
6. Sign and Magnitude. In this convention, only bits 0 through n-2 are used to hold the number, bit n-1 (the most significant bit) is used to specify the sign of the number. A zero in position n-1 means that the number is positive, a one in position n-1 means that the number is negative. In an eight bit register, for example, bit 7 represents the sign. The number 001111002 is the number + 6010 (the same as if the number were unsigned), and 101111002 is - 6010 (this would be 18810 if it were an unsigned number). The range of values able to be
represented in this form is -(2n-1-1) through +2n-1-1. The values in a 16 bit register are -32767 thru +32767. One problem with this convention is that 010 has two representations: 00000002 and 100000002.
7. 2's complement. Both 2's complement and 1's complement may be used to incorporate both positive and negative number representations in a computer; we will focus on 2's complement for reasons that will become clear later. As in the previously mentioned conventions, positive numbers (and zero) are represented normally, just as if they were unsigned binary numbers. However, the negative of a number in 2's complement form is simply the value you get when you subtract the positive representation of the number from 2n. That is, if x is binary n-bit number, then
-x = 2n - x [N10]
There is a simple way to perform this operation without actually having to do subtraction, consisting of a two step process as follows:
i. Complement the value. That is, change every 1 to a 0 and every 0 to a 1.
ii. Add 1 to the result of the first step.
For example, in an eight bit register, the number +5610 is given by 001110002. Complementing this gives 110001112. Adding 1 gives 110010002 which is used to represent -5610.
Recall that when converting a binary number to a decimal number, we simply add up all the weighted values of the positions in the binary where there is a 1. To convert a 2's complement number, the same procedure is applied, but the weight
of the (n-1)st bit (the most significant bit) has a weight of -2n-1 instead of +2n-1!.
Thus the decimal value of the four-bit 2's complement number 1110 is -23 + 22 + 21 = -8 + 4 + 2 = -2.
The range of values able to be represented in 2's complement is -2n-1 through +2n-
1-1. This is one more negative value than is represented by the sign & magnitude representation, corresponding to the fact that there is only one representation for zero.
Note 1: When this convention is being used, all numbers are considered ‘2's complement’ numbers, regardless of whether they are positive or negative. Don’t fall into the trap of thinking that only the negative numbers are in 2's complement.
Note 2: Negative numbers can be converted to positive numbers using the exact same procedure: complement and add 1 (i.e. you don’t have to do the operations in reverse by subtracting 1 and then complementing).
Note 3: Negative numbers will always have a leading 1, positive numbers will always have a leading 0. If you are asked to convert a positive decimal number to a 2's complement number, don’t forget to add the leading 0!
Note 4: When a positive n-bit number and its negative representation (in 2's complement form) are added together, the result is 2n (see N10]). Since the maximum value an n-bit register can hold is 2n-1, the result is all zeros in the register and an ‘overflow’ (This is an addition modulo 2n). We ignore the overflow and get the correct result: zero. As we will see this is important for implementing binary arithmetic.
Note 5: A 2's complement integer n bits in length can be represented similarly to [N8] except that the weight of the most significant bit is negative.
N = (- dn-1 x -2n-1) + di2i [N11]
Radix Conversion of 2's complement numbers.
Decimal to 2's Complement Conversion
Converting a positive decimal number to 2's complement form is identical to converting it to an unsigned binary number, except that you must be sure to include a leading zero.
Example 14: Represent +1910 in 2's complement notation
Value of
bit n-1 Value of remainin g bits
Example
: +19 Example : -19
Unsigned dn-12n-1 diri 010011 n/a Sign &
Magnitude sign diri 010011 110011
2's Complement -dn-12n-1 diri 010011 101101 Table TN2.
+1910 = 0100112 , not 100112 (which would be - 13) To convert a negative decimal number to 2's complement form
a. Convert the number to binary, assuming it is positive, as above;
b. Use the complement-and-add procedure to convert the positive 2's complement number into a negative 2's complement number.
Example 15: Represent -1910 in 2's complement notation.
a. +19 = 010011 (remember to include the leading 0!) b. complement and add: 101100 + 1 = 1011012 2's Complement to Decimal Conversion
To convert a binary 2's complement number (regardless of sign) to decimal, use [N11].
That is, sum up the weights of all the positions containing a one, except that the weight of the most significant bit is negative. Example:
101101 = -25 + 23 + 22 + 20 = -32 + 8 + 4 + 1 = -32 + 13 = - 1910
Table TN2 summarizes these number formats and includes the previous examples.
Notice that these formats differ only in the interpretation of the most significant bit position (2n-1), and that positive numbers have the same representation regardless of format (although the leading 0 is not required if the number is known to be in unsigned format.)
Practice problems - 2's Complement Numbers:
1. Given the binary number 11011011, what is its decimal value if it is a a. unsigned binary number (Ans: 219)
b. sign and complement number (Ans: -91) c. 2's complement number (Ans: -37)
2. Convert +24 into a 2's complement number (Ans: 011000 Note the leading zero!)
3. Convert -24 into a 2's complement number (Ans: 1010000)
4. Convert -1 into a 2's complement number (assume a 4-bit result) (Ans: 1111)
Note that -1 is 11111...2 regardless of the size of the register. Note also that any positive or negative 2's complement number can have its leading digit propagated to the left without changing the number.
5. What is the minimum (maximum negative) number which can be
represented using a 6-bit 2's Complement representation? What is the maximum (positive) number?
6. What is the minimum (maximum negative) number which can be
represented using a n-bit 2's Complement representation? What is the maximum (positive) number?
Convert, if possible, the following decimal numbers to 2's complement assuming an 8-bit binary representation for all.
8. 1 11. -1 14. 72
9. 127 12. -127 15. -105
10. 255 13 -255 16. -6
Convert the following 2's complement numbers to decimal.
17. 1111 20. 11110000
18. 001101 21. 11111111
19. 0000000 22. 01
4In practice, the bias is general chosen to be 1 less then the values presented here. For instance, as we will see, the IEEE single-precision floating point specification, which uses a biased 8 bit number for the exponent representation uses a bias of 127, not 128.
binary unsigned Bias(3) 000 0 -3 001 1 -2 010 2 -1 011 3 0 100 4 1 101 5 2 110 6 3 111 7 4
• Biased numbers - It is sometimes desirable (we’ll see an example in a bit) to have the range of negative and positive numbers by ‘monotonically’ increasing - that is, each successive number is determined by adding one to the previous number. Basically, we take our numerical sequence, and ‘shift’ the meanings of the combinations of ones and zero so that some of the combinations are negative in interpretation.
For example, using three bit binary numbers:
Notice that the decimal values have been ‘shifted’ down (or ‘biased’ by) 3 positions, eliminating the values 5 thru 7 and inserting the values -3 thru -1.
The value of the bias is usually chosen to be roughly one half of the total range of numbers available, which is determined by the number of bits allocated to the number. In the above example, three bits give a total of eight numbers, 0 thru 7;
we would generally choose a bias of three or four, depending on whether we wanted more positive or negative numbers. In general, for the purposes of this text, choose a bias as follows:
1. Given the maximum number of binary bits, n, choose a bias4 = 2n/2 = 2n-1 If n = 8, the bias should be 128 ( = 28/2 = 256/2 = 128 = 28-1 = 27 If n = 16, the bias should be 216/2 = 32K
2. Given a negative decimal number, assume a bias equal to the minimum power
of two greater than the absolute value of the negative decimal number. Also, if 2m is the bias, then the number of bits in the biased representation must be m+1.
This is because, if the bias is 2m, then the range of numbers to be represented is - 2m-1 thru +2m for a total range of 2 x 2m = 2m+1. This requires m+1 bits in the binary biased representation.
If the negative decimal number is -23 assume a bias equal to 32. The number of bits in the biased representation will be 6.
If the negative decimal number is -129 assume a bias equal to 256. The number of bits in the biased representation will be 9.
Converting Decimal numbers to Biased binary numbers 1. Add the bias to the decimal number
2. convert the resulting decimal number to binary Examples:
16. Assume a five-bit biased binary representation. What bias would you use?
(Ans. 15 or 16, usually 15).
17. Using this bias, convert the decimal numbers 0, 6, and -12 to biased format.
Ans: 0 + 15 = 15 = 011112 6 + 15 = 21 = 101012 -12 + 15 = 3 = 000112
18. Convert -49 to biased binary representation.
Choose a bias equal to 2m such that 2m > 49. In this case, the bias = 64.
Then
a. Add the bias: -49+64 = 15
b. Convert the result to binary: 1510 = 00011112
Notice that, since 2m = 64, m = 6 and the binary representation must contain m+1 bit positions (7 in this case).
Converting biased binary numbers to decimal
1. Convert the biased binary number to decimal
2. Subtract the bias from the resulting decimal number Examples:
19. Convert the biased binary number 111111 to decimal
Since there are 6 binary digits, assume the bias is 26/2 = 25 =32 1. Convert to decimal: 1111112 = 6310
2. Subtract the bias: 63 - 32 = 31.
20. Convert the biased binary number 00101011 to decimal.
Since there are 8 bits, assume a bias of 128 1. 001010112 = 4310
2. 43 - 128 = -85 21. Convert 0001 to decimal.
Bias = 8 0001 = 1 0 - 8 = -710
Practice Problems - Biased Binary Numbers
1. What bias should you choose for biased binary representations with each of the following number of bits?
a. 8 bits b. 2 bits c. 24 bitsd. 13 bits
2. What bias should you choose for biased binary representation of each of the following decimal numbers?
a. -10 b. -1013 c. -342 d. +75
3. Convert the following decimal numbers to the appropriate biased binary format.
a. -34 b. -12 c. +245 d. -129
4. Convert the following biased binary numbers to decimal
a. 101 b. 00011 c. 111111 d. 0000
Table TN3 shows the decimal values associated with the binary numbers 0000 through 1111 in each of the binary number representations discussed.
UNSIGNED
BINARY UNSIGNED
DECIMAL SIGN &
MAGNI- TUDE
COMPL-2'S EMENT
BIAS(8) BIAS(7)
0000 0 0 0 -8 -7
0001 1 1 1 -7 -6
0010 2 2 2 -6 -5
0011 3 3 3 -5 -4
0100 4 4 4 -4 -3
0101 5 5 5 -3 -2
0110 6 6 6 -2 -1
0111 7 7 7 -1 0
1000 8 -0 -8 0 1
1001 9 -1 -7 1 2
1010 10 -2 -6 2 3
1011 11 -3 -5 3 4
1100 12 -4 -4 4 5
1101 13 -5 -3 5 6
1110 14 -6 -2 6 7
1111 15 -7 -1 7 8
Table TN3.
Note:
The above representations of binary numbers seem to refer to only integers. However, a ‘binary point’ (as opposed to a decimal point) could be assumed to be anywhere in the register holding the number. If we have an eight bit (1 byte) register containing the bits 10100111, it might, depending on the application, be interpreted to contain
101001112 = 16710, or 1010011.12 = 83.510, or 1010.01112 = 10.437510
These are called Fixed Point numbers, in contrast to floating point numbers which will be discussed shortly.
Note that shifting a binary number left (and inserting a zero into the least significant digit) is the same as multiplying by 2. Compare this to adding a low order zero to a decimal number. Shifting right performs the DIV 2 operation, since the least significant
bits are shifted out of the register and are lost.
0110112 = 2710 1101102 = 5410 0110112 = 2710