| ISSUE 301/456 · COMPUTER SCIENCE |
~4 MIN |
FOUNDATION / SOFTWARE SYSTEMS · READING DATE 2027-07-29 · JULY 2027 — SOFTWARE AND FLUIDS
Why the same two bytes can mean two different numbers
START HERE
The digits 12 and 21 use the same symbols but mean different numbers. A byte is eight digits, each 0 or 1, together able to encode an integer from 0 to 255. Endianness is the agreed order of the bytes making up a multi-byte value in memory or a data format. Identical bytes can describe different numbers if two readers assume different orders.
|
/ WHY NOW
Two computers can receive identical bytes and disagree about the number if they assume different orders. The information was not damaged in transit; the interpretation rules did not match.
|
/ THE IDEA
A two-byte unsigned integer uses one byte for the groups of 256 and another for the remainder. The more significant byte contributes multiples of 256. The less significant byte contributes the units from 0 through 255. Big-endian order places the more significant byte first, at the lower numbered address or earlier file position. Little-endian order places the less significant byte first. Neither reverses the ordinary meaning of arithmetic; they choose different layouts for storing the pieces of a value.
THE FORMAL IDEA
value = 256 × high byte + low byte
| “High” means more significant, not higher memory address. The stored bytes and the reconstructed value are unitless integers in this example. | | Unsigned means no part of this representation is being used to encode a negative sign. Actual formats must specify signedness and width as well as byte order. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
The value 258 = 256 × 1 + 2 needs high byte 1 and low byte 2. Big-endian stores [1, 2]; little-endian stores [2, 1]. Keep received bytes [1, 2], but change only the reader’s assumption to little-endian. The reader calculates 256 × 2 + 1 = 513 instead of 258.
|
The mismatch can yield a plausible-looking but wrong value rather than an obvious error. A count, file length or sensor reading can therefore be misinterpreted even though every transmitted bit arrived correctly. A test using zero or repeated bytes might accidentally conceal the mismatch.
/ SO WHAT?
For example, [1, 1] decodes to 257 in either order. A useful format test includes deliberately asymmetric values such as 258. The sender and receiver should follow an explicit format, not rely on whichever layout their own machine happens to use internally. Conversion changes the representation while preserving the intended number.
ONE CAVEAT |
| This concerns byte order within multi-byte values, not the order of characters in ordinary text and not automatically the transmission order of bits inside a byte. Other encoding rules remain separate. |
KEEP THIS
Identical bytes produce identical numbers only when readers agree on the value’s width, signedness and byte order.
|
NEXT: How water can boil without being heated to 100°C
|