Sub topic 2
Unsigned Integers and Their Range
This chapter assumes you can convert small numbers between decimal and binary and know the powers of two up to 2^10. It builds on the positional-systems chapter and prepares you for signed encodings, where the same bit patterns are read differently.
What an unsigned integer is
An unsigned integer is a binary number with no sign. Every bit contributes a non-negative weight, so the value is never below zero. An n-bit pattern b(n-1) ... b1 b0 has the value:
value = b(n-1) × 2^(n-1) + ... + b1 × 2^1 + b0 × 2^0
Bit 0 is the least significant bit (LSB). Bit n-1 is the most significant bit (MSB). The width n is fixed by the hardware or the type, so leading zeros are part of the pattern: 00000101 and 101 are the same value, but only the first one is an 8-bit pattern.
Worked example: decode 10110101 as an 8-bit unsigned integer.
- Bits set: positions 7, 5, 4, 2, 0.
- Value: 128 + 32 + 16 + 4 + 1 = 181.
Count and range
n bits give 2^n distinct patterns. Unsigned encoding assigns them to the consecutive values 0 up to 2^n − 1. The range is therefore:
0 ≤ x ≤ 2^n − 1
The maximum, 2^n − 1, is the all-ones pattern. It is one less than the count because zero uses one of the patterns.
| Width n | Count 2^n | Largest value 2^n − 1 | Hex of largest |
|---|---|---|---|
| 4 | 16 | 15 | F |
| 8 | 256 | 255 | FF |
| 16 | 65,536 | 65,535 | FFFF |
| 32 | 4,294,967,296 | 4,294,967,295 | FFFFFFFF |
| 64 | 18,446,744,073,709,551,616 | 18,446,744,073,709,551,615 | FFFFFFFFFFFFFFFF |
Hexadecimal is a convenient check. One hex digit is exactly 4 bits, so an n-bit all-ones value is n/4 letters F.
Powers of two to memorize
GATE questions rely on fast mental arithmetic with powers of two:
- 2^10 = 1,024 (K), 2^20 = 1,048,576 (M), 2^30 (G), 2^40 (T).
- To compute 2^n for larger n, split the exponent: 2^26 = 2^6 × 2^20 = 64 M.
Bits needed for a given value
The reverse question is how many bits you need to store a value M. You need the smallest n with 2^n − 1 ≥ M, which is the same as 2^n > M:
n = ⌊log2(M)⌋ + 1 for M ≥ 1, and n = 1 for M = 0
Python's int.bit_length() computes exactly this.
Examples:
- M = 1000: since 2^9 = 512 ≤ 1000 < 1024 = 2^10, you need 10 bits.
- M = 1024: this is exactly 2^10, so it needs 11 bits. Ten bits stop at 1023.
- Storing N distinct items numbered 0 to N−1 needs ⌈log2 N⌉ bits. For N = 1024 that is 10 bits, because the largest label is 1023.
The two formulas differ because one counts values up to M and the other counts items. Check which one a question asks for.
Wraparound and modular arithmetic
An n-bit register cannot hold a result outside 0 to 2^n − 1. Hardware keeps only the low n bits, which is arithmetic modulo 2^n. A 3-bit counter makes the cycle visible.
Two consequences matter in practice:
- Adding 1 to the maximum value gives 0. This is how a counter or a loop variable "rolls over".
- Subtracting 1 from 0 gives 2^n − 1. This is a classic bug when a loop such as
for (unsigned i = n - 1; i >= 0; i--)never ends, becausei >= 0is always true for an unsigned value.
Carry and borrow as unsigned overflow flags
For unsigned addition, an overflow means the true sum exceeds 2^n − 1. The adder shows this as a carry out of the MSB position. If the carry-out is 1, the n-bit result is wrong by exactly 2^n.
For unsigned subtraction a − b, the result is invalid when a < b. That is called a borrow. Many adders compute a − b as a + (~b) + 1, so a carry-out of 1 there means no borrow occurred. A carry-out of 0 means a borrow occurred. Some processors invert this flag for subtraction, so check the convention before relying on it.
The following code models these rules for any width.
def unsigned_range(n):
return 0, (1 << n) - 1
def bits_needed(m):
if m < 0:
raise ValueError("unsigned values cannot be negative")
return max(1, m.bit_length())
def add_unsigned(a, b, n):
mask = (1 << n) - 1
total = a + b
return total & mask, total >> n # (n-bit result, carry out)
def sub_unsigned(a, b, n):
mask = (1 << n) - 1
return (a - b) & mask, a < b # (n-bit result, borrow)
print(unsigned_range(8)) # (0, 255)
print(bits_needed(1000), bits_needed(1024), bits_needed(0)) # 10 11 1
print(add_unsigned(200, 100, 8)) # (44, 1)
print(sub_unsigned(5, 10, 8)) # (251, True)Check the first result by hand: 200 + 100 = 300, and 300 − 256 = 44 with a carry of 1. For the subtraction, 5 − 10 = −5, and −5 + 256 = 251 with a borrow.
Unsigned integers in memory and addresses
Unsigned integers are the natural type for quantities that cannot be negative: sizes, counts, array indexes and memory addresses.
- A k-bit address can name 2^k locations. A byte-addressable memory with 32-bit addresses holds 2^32 bytes, which is 4 GB. The highest address is 2^32 − 1 =
0xFFFFFFFF. - A memory of 2^m bytes needs m address bits. For 64 KB = 2^16 bytes, m = 16, and the last address is
0xFFFF. - A memory of M words with W bits per word has M × W bits total, and M needs ⌈log2 M⌉ address bits. The word width does not change the address width.
Shifts and multiplication by powers of two
In an unsigned integer, shifting left by one position doubles the value, provided no 1 bit is pushed out of the MSB. Shifting right by one position performs integer division by 2 and drops the LSB, which is the remainder.
00001101(13) shifted left by 2 gives00110100(52 = 13 × 4).10110101(181) shifted right by 3 gives00010110(22 = ⌊181 / 8⌋).- Shifting left by k loses information if any of the top k bits is 1. That is the same overflow condition as before.
This is also why converting binary to hex works by grouping: a group of 4 bits is a base-16 digit, and each group's weight differs by a factor of 2^4.
Common errors
- Off-by-one in the maximum. The largest n-bit unsigned value is 2^n − 1, not 2^n. The count is 2^n.
- Ignoring the width. "Is 300 representable?" depends on n. In 8 bits it is not, but in 9 bits it is.
- Reading the same pattern both ways.
11111111is 255 as unsigned, and it is a different value under a signed interpretation. The bits do not carry their own type, so the question must say which interpretation applies. - Treating carry as signed overflow. Carry-out detects unsigned overflow only. Signed overflow uses a different rule, which a later chapter derives.
- Mixing signed and unsigned in C comparisons. In C, comparing a signed negative value with an unsigned value converts the signed one to unsigned, so −1 becomes 2^n − 1 and compares as larger than most values.
Exercises
Exercise
What is the minimum number of bits needed to store every unsigned integer from 0 to 5000, and what is the largest value that width can hold?
Show answer
2^12 = 4096 is below 5000 and 2^13 = 8192 is above it, so 13 bits are needed (⌊log2 5000⌋ + 1 = 12 + 1). A 13-bit unsigned field holds 0 to 2^13 − 1 = 8191.
Exercise
Two 8-bit unsigned registers hold 0xC8 and 0x64. What 8-bit result and carry-out does an adder produce for their sum, and is the result correct as an unsigned value?
Show answer
0xC8 = 200 and 0x64 = 100, so the true sum is 300. In 8 bits this is 300 − 256 = 44 = 0x2C with a carry-out of 1. The carry shows unsigned overflow, so 0x2C is not the correct sum.
Exercise
A byte-addressable memory has 64 KB of storage. How many address bits does it need, and what is the highest address in hexadecimal?
Show answer
64 KB = 2^6 × 2^10 = 2^16 bytes, so 16 address bits are needed. The highest address is 2^16 − 1 = 0xFFFF.
Exercise
An 8-bit unsigned register holds 0x03. After computing 0x03 − 0x05, what value does it hold, and what does the borrow indicate?
Show answer
The true result is −2. Modulo 256 this becomes 254 = 0xFE. The borrow is set because the first operand (3) is smaller than the second (5), so the unsigned result is invalid.