Hexadecimal, Overflow and Round-Off
Hexadecimal
Binary code is too long in representation. Hexadecimal is much shorter. Hexadecimal uses 16 digits.Problem: we are short of numbers. Base 10 handed us ten symbols, 0 through 9, and hexadecimal needs six more. So the first six letters are pressed into service as digits: A is 10, B is 11, C is 12, D is 13, E is 14, F is 15.
Hexadecimal digits: 0, 1, 2, 3, 4 ... 9, A, B, C, D, E, F.
Every 4 bits become one hex digit
Converting a binary number to a hex number is easy. Every 4 bits can convert to a hex digit, and the reason is a coincidence worth naming: four bits have exactly 16 patterns, and hexadecimal has exactly 16 digits.So: split the bits into groups of four starting from the RIGHT, read each group as a value from 0 to 15, then write that value as its single hex digit. Groups worth 0 through 9 keep the digit they already had. The six that need a letter are these.
| Group of 4 bits | Value | Hex digit |
|---|---|---|
| 1010 | 10 | A |
| 1011 | 11 | B |
| 1100 | 12 | C |
| 1101 | 13 | D |
| 1110 | 14 | E |
| 1111 | 15 | F |
Overflow error
In many programming languages, integers are represented by a fixed number of bits, which limits the range of integer values and the mathematical operations on those values. This limitation can result in overflow or other errors.For example in Java, the range of the value of an integer is from -2,147,483,648 to +2,147,483,647. Compute a result bigger than that and the value overflows, with no error message at all. Add 1 to 2,147,483,647 and the result wraps around to -2,147,483,648, and the program carries on with that wrong value. In other languages, an overflow stops the program with an error.
Some languages like Python are different: integers do not have limits on number size but instead expand to the limit of the available memory. Python floats are represented with 64 bits.
The AP exam's pseudocode, the language of the exam reference sheet, works the same way. Its integers are limited only by the size of the computer's memory, so an integer in an exam question never overflows.
The largest value in n bits
An unsigned integer stores only whole numbers from 0 upward. In n bits, its largest value is 2n - 1. Java's int is signed: it spends half of its 232 patterns on negative numbers. So its largest value is 2,147,483,647, which is 2^31 - 1.With 4 bits, the largest integer that can be stored is two to the fourth, minus one, which is 15. A 4-bit integer can hold any value in {0, 1, 2, ..., 15}. Storing the value of 10 + 6 = 16 would overflow, since 16 is 10000 in binary and that requires at least 5 bits.
If x is a 3-bit integer, storing 7 + 7 in x overflows. In binary that is 111 + 111, and the sum 1110 requires at least 4 bits.
Round-off errors
A fixed number of bits is used to store real numbers, which limits their range and the arithmetic on them. Because of this limitation, round-off errors and other errors can occur, and some real numbers can only be stored as approximations.Python computes 1/3 as 0.3333333333333333. This value is only an approximation of 1/3, which is an infinitely repeating decimal. The bits run out long before the digits do. So the value is rounded to the nearest one the bits can hold, and the gap between the two is round-off error.
The same thing happens to numbers that look tidy in base 10. In binary, 0.1 is a repeating fraction, 0.000110011001100... with the 0011 going on forever. Python stores the nearest 64-bit value instead, a hair more than 0.1. Add it up ten times and the small errors show:
total = 0
for i in range(10):
total = total + 0.1
print(total)
That prints 0.9999999999999999 where 1 was expected.
The fixed width limits precision as well. A float keeps only about 16 significant digits. Once a float is large enough, adding 1 to it can leave it unchanged, because the sum rounds back to the number it started from.
The program below doubles a float,
x, until adding 1 to it no longer changes it, and stops after 100 doublings if that never happens. Run it, then change it and run it again:• Change
doublings < 100 to doublings < 36.• Press Reset, then change
x = 1.0 to x = 1, so that x is an integer.x = 1.0
doublings = 0
while x + 1 != x and doublings < 100:
x = x * 2
doublings = doublings + 1
print("Doublings:", doublings)
print("x:", x)
print("x + 1:", x + 1)
print("Same value:", x + 1 == x)
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.