Sub topic 1
Positional Number Systems and Base Conversion
Prerequisites
This chapter assumes you know decimal place value, can add binary numbers by hand, and understand powers of two. You should also be comfortable reading a 4-bit or 8-bit binary pattern.
Positional number systems
A positional number system gives each digit a weight based on its place. In base b, the digit at position i (counting from 0 on the right) has weight b^i. So 1011 in base 2 equals 1x8 + 0x4 + 1x2 + 1x1 = 11. Hexadecimal (base 16) and octal (base 8) are used because each hex digit maps to exactly four bits, which makes long binary strings easier to read.
Unsigned integers and their range
An n-bit unsigned integer uses all n bits as magnitude. The smallest value is 0 (all zeros) and the largest is 2^n - 1 (all ones). An 8-bit unsigned value therefore ranges from 0 to 255.
Signed integers: sign-magnitude, 1's and 2's complement
Three encodings are common for signed integers:
| Encoding | Negative of 5 in 4 bits | Range for n bits |
|---|---|---|
| Sign-magnitude | 1101 | -(2^(n-1) - 1) to 2^(n-1) - 1 |
| 1's complement | 1010 | -(2^(n-1) - 1) to 2^(n-1) - 1 |
| 2's complement | 1011 | -2^(n-1) to 2^(n-1) - 1 |
Sign-magnitude uses the top bit as a sign and has two zeros. 1's complement inverts every bit to negate, which also gives two zeros. 2's complement inverts every bit and adds 1, which gives one zero and one extra negative value. Modern hardware uses 2's complement.
2's complement arithmetic and overflow
Addition works the same way for signed and unsigned values in 2's complement. Subtraction becomes addition of the negated operand. Overflow occurs when the result falls outside the range. For addition, overflow happens exactly when the carry into the sign bit differs from the carry out of it.
For example, 7 + 1 in 4 bits gives 0111 + 0001 = 1000. The carry into the sign bit is 1 and the carry out is 0, so overflow is detected. The result 1000 reads as -8, which is wrong.
def to_twos(x, n):
lo, hi = -(1 << (n - 1)), (1 << (n - 1)) - 1
if not lo <= x <= hi:
raise OverflowError(f"{x} does not fit in {n} bits")
return x & ((1 << n) - 1)
print(format(to_twos(-5, 4), '04b')) # 1011Converting fractions between decimal and binary
To convert a fraction, multiply by 2 repeatedly and record the integer part each time. Then keep the fractional part and repeat.
def to_binary_fraction(x, bits):
out = ""
for _ in range(bits):
x *= 2
bit = int(x)
out += str(bit)
x -= bit
return out
print(to_binary_fraction(0.625, 3)) # 101
print(to_binary_fraction(0.1, 8)) # 00011001Many decimal fractions, such as 0.1, produce a repeating binary pattern. A finite number of bits can only approximate them.
> Note: A decimal fraction terminates in binary only if its denominator, after reduction, is a power of 2.
Exercises
Exercise
Write -9 as an 8-bit 2's complement pattern.
Show answer
9 is 00001001. Invert to get 11110110 and add 1 to get 11110111.
Exercise
In 4-bit 2's complement, does -8 + (-1) overflow?
Show answer
Yes. The true result is -9, which is below the minimum of -8. The sign bits carry in and out differently, so overflow is detected.