Bits, integers and floats
Every value is a bit pattern plus a rule for reading it. Learn the three rules that matter: unsigned, two's complement and IEEE 754.
A byte is eight bits. The bits mean nothing until something agrees how to read them. The pattern 11111011 is 251 as an unsigned byte, −5 as a signed byte, and part of a UTF-8 sequence in a string. The hardware does not care. Your code decides.
Two's complement: why −1 is all ones
Signed integers use two's complement. The top bit carries a negative weight: in an 8-bit value it is worth −128 instead of +128. To negate a number, flip every bit and add one. The payoff is that addition works the same way for signed and unsigned values, so the CPU needs only one adder.
Flip bits, read the same byte three ways
- Unsigned
- 5
- Signed (two's complement)
- 5
- Hex
- 0x05
Signed: 4 + 1 = 5
Overflow is not an error in most languages. In C, signed overflow is undefined behaviour, which lets the compiler assume it never happens. In Rust, debug builds panic and release builds wrap. In JavaScript, bitwise operators silently convert to 32-bit integers: 2 ** 31 | 0 is -2147483648.
IEEE 754: floats are fractions in base 2
A 64-bit double stores one sign bit, 11 exponent bits and 52 fraction bits. It can represent 0.5, 0.25 and 0.125 exactly, but not 0.1, for the same reason 1/3 has no finite decimal form. Every decimal you type is rounded to the nearest representable binary fraction.
Inside the double that stores 0.1
- Stored as hex
- 0x3FB999999999999A
- Exact integers up to
- 253
- 0.1 + 0.2
- 0.30000000000000004
0.1 + 0.2 // 0.30000000000000004
0.1 + 0.2 === 0.3 // false
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON // true
// Integers are exact only up to 2^53
Number.MAX_SAFE_INTEGER // 9007199254740991
2 ** 53 + 1 === 2 ** 53 // true: the +1 was rounded away
// A 64-bit ID from an API, parsed as JSON
JSON.parse('{"id": 1234567890123456789}').id
// 1234567890123456800 (wrong last digits)Many APIs send 64-bit IDs as strings for exactly this reason.
Endianness: which byte comes first
A 32-bit integer such as 0x12345678 occupies four bytes. Big-endian stores 12 34 56 78, most significant byte first. Little-endian stores 78 56 34 12. x86 and almost all Arm systems run little-endian, while network protocols traditionally use big-endian, called network byte order. You meet this the moment you parse a binary header or write bytes to a socket.
| Value | Big-endian bytes | Little-endian bytes |
|---|---|---|
| 0x12345678 | 12 34 56 78 | 78 56 34 12 |
| 1 (uint16) | 00 01 | 01 00 |
| −2 (int16) | FF FE | FE FF |
Where you will meet it
X (formerly Twitter) and Discord IDs exceed 2^53, so their APIs return them as strings. Parsing them as numbers corrupts the last digits.
Payment systems such as Stripe represent amounts in the smallest currency unit, as integers, so rounding never creates or destroys money.
TCP headers, Protobuf fixed-width fields and image formats all specify byte order. Reading them with the wrong order gives plausible but wrong numbers.
Check yourself
Answer out loud before you open each one.
What is −1 as an 8-bit two's complement byte?
11111111, which is 255 unsigned. Flip 00000001 to 11111110, then add one.
Why does 0.1 + 0.2 not equal 0.3?
Neither 0.1 nor 0.2 has an exact binary form. Each is rounded, and the rounding errors add up to a value one unit above the double nearest 0.3.
What is the largest integer a JavaScript number holds exactly, and why?
2^53 − 1. A double has 52 stored fraction bits plus an implicit leading 1, so 53 bits of precision.
Try it
In your browser console, run new DataView(new Uint32Array([0x12345678]).buffer).getUint8(0).toString(16). A result of 78 means your machine is little-endian.