ARITHMETIC WORKSHEETS

Checking an unsigned eight-bit wrap against an exact sum

An exact sum and a fixed-width stored value are two different results. This guide adds 250 and 10 exactly, then shows what an unsigned eight-bit field holds under a modulo-256 convention.

Two answers to one addition

Adding 250 and 10 on a calculator gives 260. That is the mathematical sum, and it is correct as an integer. A storage field that holds an unsigned eight-bit value can represent only the integers from 0 to 255, so 260 does not fit. What happens next depends on the system that stores the value. Some systems wrap around, some report an error, and some saturate at the largest value. This guide keeps the exact result and the stored result apart, because treating the stored value as the sum is how overflow bugs begin. The basic calculator in this worksheet always returns the exact integer result.

The wrapping convention

One common convention reduces the exact sum modulo 256, which means subtracting multiples of 256 until the result lies between 0 and 255. For 260, one subtraction of 256 leaves 4. The second case below enters 260 minus 256 and gets 4. The number 256 is 2 to the eighth power, the count of distinct patterns that eight bits can form. Under this convention, 250 plus 10 is stored as 4, and the difference of 256 between the exact and stored values is the part that did not fit. A subtraction can wrap in the other direction: 5 minus 10 is negative 5 exactly, and adding 256 brings it back into range as 251. The rule is the same remainder after division by 256, with the remainder taken to be nonnegative. This is arithmetic on a clock face with 256 positions, where going past 255 returns to 0.

The exact sum in another base

The integer base converter changes the way a number is written without changing its value. Converting 260 from base 10 to base 16 gives 104, which the third case records. The hexadecimal digits 1, 0 and 4 are three digits, and two hexadecimal digits are exactly one byte, so 104 does not fit in a byte. The leftmost digit stands for 256, and the remaining 04 is the value 4 that the wrapped result keeps. Written in binary, 260 is 100000100, nine bits, with the ninth bit lost when the value is truncated to eight. The converter keeps every digit, so it shows the exact value rather than a byte-sized truncation.

Other sums that wrap

Two more cases show the pattern. Adding 200 and 100 gives 300, and 300 minus 256 is 44, so the wrapped value is 44. Adding 255 and 1 gives 256, and the wrapped value is 0, since 256 minus 256 is zero. That last case is the clearest picture of a counter rolling over from its largest value to its smallest. A sum large enough to pass 512 needs two subtractions, so the general rule is to take the remainder after dividing by 256. For the small examples here a single subtraction is enough, but the remainder rule is the one that stays correct for any sum.

Signed eight-bit values behave differently

Signed eight-bit integers in two's complement span negative 128 to 127. Under the same wrapping idea, adding 1 to 127 gives 128, which wraps to negative 128. The bit pattern is the same as the unsigned pattern for 128, and the interpretation differs. This is why the worksheet states that its example uses an unsigned field. The same stored pattern can mean different numbers depending on whether the field is read as signed or unsigned, and only the declared type settles which. A bare byte value without a stated type is ambiguous, in the same way that a fixed-point integer without a scale is.

Systems differ in what they do on overflow

Wrapping is not universal. Many hardware instructions wrap by default. Some languages define wrapping for unsigned types and treat signed overflow as undefined behavior. Some languages and libraries raise an error on overflow, which is called checked arithmetic. Others saturate, clamping to the largest representable value, as audio and image libraries often do, so that 260 would be stored as 255. Python integers and JavaScript BigInt values grow as large as needed and never wrap. Because these behaviors differ, a result about stored values should always name the target system. This worksheet supplies one mathematical convention and makes no claim about any particular language.

Why saturation and wrapping give different errors

Consider a pixel brightness of 250 increased by 10. Wrapping stores 4, turning a bright pixel nearly black, an obvious and ugly error. Saturating stores 255, a small and harmless deviation. For a counter, the choice reverses: wrapping a packet counter from 255 to 0 is expected and handled, while saturating would freeze the count. Neither choice is better in general. The decision depends on what the number means, and the exact sum of 260 is the reference against which either behavior can be judged. Knowing the exact value makes the size of the error visible.

Choosing a wider type

The most direct way to avoid overflow is to store the result in a wider field. A 16-bit unsigned field holds 0 to 65,535, which easily contains 260. Adding the two eight-bit values in a 16-bit accumulator and then checking the range before narrowing is a standard practice in careful code. The worksheet's exact calculator plays the role of that wider accumulator. The decision to narrow the result then becomes an explicit step with a defined policy, instead of an accident of the storage type. For sums of many values, the width needed grows with the count, one extra bit for each doubling of terms.

Checking a stored value against the exact sum

To audit a result, compute the exact sum, compare it with the range of the field, and apply the system's stated overflow rule. If the exact sum is within range, the stored value must equal it. If not, the stored value should equal the exact sum minus the appropriate multiple of 256 under wrapping, or the maximum under saturation. A mismatch means the system uses a different rule than assumed. With 250 plus 10, a stored 4 confirms wrapping, 255 confirms saturation, and an error confirms checked arithmetic. Three numbers decide which behavior is in effect. The exact sum, the field width and the stored value are enough to infer the rule, and recording all three in a test makes the behavior explicit for the next person who reads the code. A test with only the stored value would pass for either of two different rules whenever the exact sum happens to fit.

Reproduce the checks

Exact addition

Open the Basic calculator and enter:

First number
250
Operation
add
Second number
10

Expected output:

Result

260

The mathematical result is retained.

Supplied wrapping convention

Open the Basic calculator and enter:

First number
260
Operation
subtract
Second number
256

Expected output:

Result

4

One modulus is removed in this small example.

Exact result in hexadecimal

Open the Integer Base Converter and enter:

Value
260
Source base
10
Target base
16

Expected output:

Value

104

The converter preserves the full integer rather than a byte-sized truncation.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks