An unsigned binary integer is simply a binary fixed-point system with no fractional digits.
Unsigned binary integers are modular number systems, where the modulus is usually a power of 2.
Example 15.7. A 4-bit unsigned binary number system
A 4-bit unsigned binary number has values ranging from 00002 (010) to 11112 (1510). Hence, it is a modulo-100002, or modulo-1610.
We identify bits in a binary integer by the power of 2 at that position. Hence, the rightmost bit is called "bit 0", the next one to the left is "bit 1", and so on. The leftmost bit will have a power of N-1, where N is the number of bits, since we start at 0. In 1011, bit 0 is 1, bit 2 is 0, and bit 3 is 1. there is no bit 4.
Modern computers typically support binary integers of 8, 16, 32, and 64 bits. A 64-bit computer supports all of these integer sizes, a 32-bit computer supports all but 64-bit, etc.
The largest value in any unsigned binary integer system is the one containing all 1s, just as the largest decimal number is the one containing all 9s.
Largest modulo-100010 = 99910.
Largest modulo-10002 = 1112.
If we add 1 to a 4-bit binary value of all 1s, note what happens:
111 1111_2 + 1 -------- 10000_2 = 2^4
This is the same thing that happens when we add 1 to 999910.
111 9999_10 + 1 -------- 10000_10 = 10^4
If we add one to the largest value possible in 4 bits, we get 24 (which requires 5 bits). If we add 1 to the largest value possible in N bits, we will get 2N. Hence, the largest value possible in N bits, is 2N - 1, just as the largest possible value with N decimal digits is 10N - 1. Table 15.7, “Unsigned Binary Integer Ranges” shows the ranges of common unsigned integer sizes.
Table 15.7. Unsigned Binary Integer Ranges
Bits | Range |
---|---|
8 | 0 to 28-1 (255) |
16 | 0 to 216-1 (65,535) |
32 | 0 to 232-1 (4,294,967,295) |
64 | 0 to 264-1 (18,446,744,073,709,551,615 = 1.844674407 * 1019) |
Note that even a 64-bit integer cannot hold Avogadro's constant, 6 * 1023. In order to represent very large numbers with only 32 or 64 bits, we must use a system other than fixed-point. This is discussed in the section called “Floating Point”.
Note also that we cannot express fractional or irrational numbers or negative numbers, but only non-negative integers. Systems for representing signed integers and non-integers are covered in later sections.
How many bits are needed to represent a number? For this we use high-school algebra. If y = bx, then x = logb(y). If the result is not an integer, we need the next higher integer since we obviously cannot have portions of bits in digital hardware. Mathematically, we use the "ceiling" function, which rounds up to the nearest integer >= N. For an N-bit number:
maximum-value = 2^N - 1 maximum-value + 1 = 2^N N = ceiling(log_2(maximum-value + 1)) = ceiling(ln(maximum-value + 1) / ln(2))
How many bits needed for 60,000?
N = ceiling(log_2(60,000 + 1)) = ceiling(ln(65,536) / ln(2)) = ceiling(15.87) = 16
For convenience, you can also use Table 15.8, “Powers of 2” and Table 15.9, “Powers of 2”. Just find the lowest power of 2 greater than the number you want to represent, and you'll immediately know how many bits it requires. The lowest power of 2 greater than 60,000 is 65,536, which is 2^16, so we need 16 bits.
Table 15.8. Powers of 2
N | 2N |
---|---|
0 | 1 |
1 | 2 |
2 | 4 |
3 | 8 |
4 | 16 |
5 | 32 |
6 | 64 |
7 | 128 |
8 | 256 |
9 | 512 |
10 | 1024 |
11 | 2048 |
12 | 4096 |
13 | 8192 |
14 | 16384 |
15 | 32768 |
16 | 65536 |
17 | 131072 |
18 | 262144 |
19 | 524288 |
20 | 1048576 |
21 | 2097152 |
22 | 4194304 |
23 | 8388608 |
24 | 16777216 |
25 | 33554432 |
26 | 67108864 |
27 | 134217728 |
28 | 268435456 |
29 | 536870912 |
30 | 1073741824 |
31 | 2147483648 |
Table 15.9. Powers of 2
N | 2N |
---|---|
32 | 4294967296 |
33 | 8589934592 |
34 | 17179869184 |
35 | 34359738368 |
36 | 68719476736 |
37 | 137438953472 |
38 | 274877906944 |
39 | 549755813888 |
40 | 1099511627776 |
41 | 2199023255552 |
42 | 4398046511104 |
43 | 8796093022208 |
44 | 17592186044416 |
45 | 35184372088832 |
46 | 70368744177664 |
47 | 140737488355328 |
48 | 281474976710656 |
49 | 562949953421312 |
50 | 1125899906842624 |
51 | 2251799813685248 |
52 | 4503599627370496 |
53 | 9007199254740992 |
54 | 18014398509481984 |
55 | 36028797018963968 |
56 | 72057594037927936 |
57 | 144115188075855872 |
58 | 288230376151711744 |
59 | 576460752303423488 |
60 | 1152921504606846976 |
61 | 2305843009213693952 |
62 | 4611686018427387904 |
63 | 9223372036854775808 |
Addition in binary works the same way as in decimal. We need only remember that our largest possible digit is 1, so when adding 1 + 1, the result is not 2, but 10, and we carry the 1. 1 + 1 + 1 is 11, so the result is 1, carry a 1.
11 111 Carry 1001 0011 + 0011 + 0111 ---- ---- 1100 1010
In fact, the process is the same for any base. We simply need to remember what our largest digit is and hence when to carry a 1.
1 27_8 19_16 + 21_8 + 21_16 -------- --------- 50_8 3A_16
Subtraction, multiplication, and division are all basically the same as in decimal as well.
What is the range of a 20-bit unsigned integer? Express your answer in binary and as an expression including a power of 2.
What are three limitations of unsigned binary?
What are bits 4 and 5 in the value 1000101010_2?
What is 0110 + 0011?