Workspace

Bits, integers and floats

3 day streakD

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.

BasicsLesson 1 of 614 min

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.

Figure 1.1

Flip bits, read the same byte three ways

−128b7
64b6
32b5
16b4
8b3
4b2
2b1
1b0
Unsigned
5
Signed (two's complement)
5
Hex
0x05

Signed: 4 + 1 = 5

Toggle bits or pick a preset. The top bit is worth −128 when read as signed. Add 1 to 127 and watch it wrap to −128: that is integer overflow.

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.

Figure 1.2

Inside the double that stores 0.1

0
sign1 bit
0 → positive
01111111011
exponent11 bits
1019 − 1023 = −4
1001100110011001100110011001100110011001100110011010
fraction52 bits
1.6000000000000000888…
value = (−1)0 × 1.6000000000000000888 × 2−4= 0.1000000000000000055511151231257827…
Stored as hex
0x3FB999999999999A
Exact integers up to
253
0.1 + 0.2
0.30000000000000004
Sign, exponent and fraction fields of 0.1. The fraction repeats 1001 forever and is rounded at bit 52, so the stored value is slightly more than 0.1.
float-surprises.jsJavaScript
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.

ValueBig-endian bytesLittle-endian bytes
0x1234567812 34 56 7878 56 34 12
1 (uint16)00 0101 00
−2 (int16)FF FEFE FF

Where you will meet it

Snowflake IDs

X (formerly Twitter) and Discord IDs exceed 2^53, so their APIs return them as strings. Parsing them as numbers corrupts the last digits.

Billing

Payment systems such as Stripe represent amounts in the smallest currency unit, as integers, so rounding never creates or destroys money.

Binary protocols

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.

A good place to start

Explore a pattern

A little guidance

Start with a pattern, read through a problem, and mark it solved when the idea clicks. Save anything you want to revisit.

New tutorials are added one at a time. Your bookmarks and solved checkmarks stay in this browser.

Explore the roadmap