### Note

There are 10 kinds of people in the world: Those who know binary, and those who don't.
##### Introduction

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 14.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.

##### Range

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 14.7, “Unsigned Binary Integer Ranges” shows the ranges of common unsigned integer sizes.

Table 14.7. Unsigned Binary Integer Ranges

BitsRange
80 to 28-1 (255)
160 to 216-1 (65,535)
320 to 232-1 (4,294,967,295)
640 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 14.8, “Powers of 2” and Table 14.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 14.8. Powers of 2

N2N
01
12
24
38
416
532
664
7128
8256
9512
101024
112048
124096
138192
1416384
1532768
1665536
17131072
18262144
19524288
201048576
212097152
224194304
238388608
2416777216
2533554432
2667108864
27134217728
28268435456
29536870912
301073741824
312147483648

Table 14.9. Powers of 2

N2N
324294967296
338589934592
3417179869184
3534359738368
3668719476736
37137438953472
38274877906944
39549755813888
401099511627776
412199023255552
424398046511104
438796093022208
4417592186044416
4535184372088832
4670368744177664
47140737488355328
48281474976710656
49562949953421312
501125899906842624
512251799813685248
524503599627370496
539007199254740992
5418014398509481984
5536028797018963968
5672057594037927936
57144115188075855872
58288230376151711744
59576460752303423488
601152921504606846976
612305843009213693952
624611686018427387904
639223372036854775808

##### Arithmetic

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.

### Note

Be sure to thoroughly review the instructions in Section 2, “Practice Problem Instructions” before doing the practice problems below.
1. What is the range of a 20-bit unsigned integer? Express your answer in binary and as an expression including a power of 2.

2. What are three limitations of unsigned binary?

3. What are bits 4 and 5 in the value 1000101010_2?

4. What is 0110 + 0011?