Bits, bytes, and data types
Everything in memory is just bits. Data types decide what those bits mean, and why 0.1 + 0.2 isn't 0.3, an int8 can wrap to -128, and an emoji can be 4 bytes long.
In the previous part we followed a program from a text file into memory. This part looks at what is actually sitting in that memory. The short answer is: bits. The longer answer is what makes data types worth understanding, because most of the strange bugs around numbers and text come from forgetting that everything is bits underneath.
Bits and bytes
A bit is the smallest unit of data: 0 or 1. A byte is 8 bits, which gives 2⁸ = 256 possible combinations, from 00000000 to 11111111.
Binary works like decimal, just with two digits instead of ten. Each position is worth twice the one to its right:
binary 1 0 1
value 4 2 1 → 4 + 0 + 1 = 5
You can check this in Python:
>>> bin(5)
'0b101'
>>> 0b101
5
>>> hex(255)
'0xff'
Hexadecimal (base 16) shows up everywhere, from colors like #ff8f4a to memory addresses, because one hex digit maps neatly onto 4 bits, so two hex digits describe exactly one byte.
The same bits, different meanings
Here is the key idea of this whole note: bits don’t know what they are. The byte 01000001 could be the number 65, or the letter A, or part of a pixel’s color. What decides its meaning is the type the program uses to read it.
>>> ord('A')
65
>>> chr(65)
'A'
>>> 'A'.encode()
b'A'
A data type is an agreement: “treat these bytes as an integer”, “treat these bytes as text”. Most bugs in this area come from the agreement being different from what you assumed.
Integers: whole numbers have limits (sometimes)
Many languages store integers in a fixed number of bits. An int8 in Go has 8 bits and can hold -128 to 127. What happens if you go past the top?
var small int8 = 127
small++
fmt.Println(small) // -128
var b uint8 = 255
b++
fmt.Println(b) // 0
The value wraps around, like an odometer rolling over. No error, no warning, just a wrong number. Real systems use 32- or 64-bit integers, so the limits are much bigger, but they still exist.
Python takes a different approach. Its documentation states that “Integers have unlimited precision”, so this just works:
>>> 2 ** 100
1267650600228229401496703205376
JavaScript is different again: ordinary numbers are floating point (see below), and integers are only exact up to Number.MAX_SAFE_INTEGER, which is 9007199254740991 (2⁵³ − 1). MDN explains that “safe” here “refers to the ability to represent integers exactly and to compare them correctly.” Past that point, things get quietly wrong:
> Number.MAX_SAFE_INTEGER
9007199254740991
> 9007199254740993
9007199254740992
> 9007199254740993n // BigInt
9007199254740993n
This is not just theory. Large numeric IDs from a database, passed through JSON into JavaScript, can lose their last digits this way. That’s why many APIs send big IDs as strings.
PHP takes yet another path. Its manual says: “If PHP encounters a number beyond the bounds of the int type, it will be interpreted as a float instead.” So the number keeps growing, but quietly turns into a float and starts losing precision:
var_dump(PHP_INT_MAX); // int(9223372036854775807)
var_dump(PHP_INT_MAX + 1); // float(9.223372036854776E+18)
Floating point: why 0.1 + 0.2 isn’t 0.3
Numbers with a fractional part are usually stored as floating point, a binary format similar to scientific notation. The catch, in the words of the Python tutorial: “most decimal fractions cannot be represented exactly as binary fractions.” In particular, “the decimal value 0.1 cannot be represented exactly as a base 2 fraction.”
So the computer stores the closest binary value it can, and tiny differences add up:
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1 + 0.2 == 0.3
False
JavaScript gives exactly the same result, because it uses the same kind of floating-point numbers. This is not a bug in either language; it’s how binary fractions work.
PHP has the same float underneath, with one extra trap: echo rounds for display, so the problem hides until you compare.
echo 0.1 + 0.2; // 0.3
var_dump(0.1 + 0.2); // float(0.30000000000000004)
var_dump(0.1 + 0.2 == 0.3); // bool(false)
The PHP manual is blunt about it: “never trust floating-point number results to the last digit, and do not compare floating-point numbers directly for equality.”
What to do about it:
- Never compare floats with
==for results of calculations. Compare with a small tolerance, or round first. - Don’t store money as floats. Use integers in the smallest unit (cents, or whole rupiah), or a decimal type:
>>> from decimal import Decimal
>>> Decimal('0.1') + Decimal('0.2')
Decimal('0.3')
Text: characters are numbers too
Text is stored as numbers as well, using an encoding that maps characters to bytes.
ASCII, the old standard, covers “a total of 128 code points”: English letters, digits, punctuation, and control characters. ASCII “encodes each code-point as a value from 0 to 127 – storable as a seven-bit integer.” That’s fine for English, but not for é, Indonesian names with special characters, Arabic script, or emoji.
Unicode gives every character in every writing system its own number (a code point). UTF-8 is the most common way to turn those numbers into bytes. According to RFC 3629, characters “are encoded using sequences of 1 to 4 octets”, and “US-ASCII characters are encoded in one octet having the normal US-ASCII value”, so plain English text is identical in ASCII and UTF-8.
That variable length is why “how long is this string?” has more than one answer:
>>> len('a'), len('a'.encode())
(1, 1)
>>> len('é'), len('é'.encode())
(1, 2)
>>> len('😀'), len('😀'.encode())
(1, 4)
Python’s len on a string counts characters; on the encoded bytes it counts bytes. Other languages count differently:
fmt.Println(len("é"), len([]rune("é"))) // 2 1 (bytes, characters)
> 'é'.length
1
> '😀'.length
2
> [...'😀'].length
1
In Go, len on a string counts bytes. In JavaScript, length counts “UTF-16 code units”, so one emoji counts as 2. None of these is wrong; they are answering different questions.
PHP’s strlen behaves like Go’s len. In the manual’s words, “strlen() returns the number of bytes rather than the number of characters in a string.” For characters, use the multibyte version:
var_dump(strlen("é"), mb_strlen("é")); // int(2) int(1)
var_dump(strlen("😀"), mb_strlen("😀")); // int(4) int(1)
Booleans
The simplest type of all: true or false. In memory a boolean usually takes a whole byte, because a byte is the smallest piece of memory most computers can address on its own. Booleans get a part of their own later in this series, when we look at conditions and logic.
Why this matters day to day
- Money. Float rounding errors in prices and totals are a classic production bug. Store amounts as integers in the smallest unit, or use a decimal type.
- IDs and JSON. Big integer IDs can silently change when parsed as JavaScript numbers. Send them as strings if they may exceed 2⁵³ − 1.
- Text limits. “Max 255 characters” and “max 255 bytes” are not the same limit once users type names with accents or emoji. Check which one your database column or API actually means (and in PHP, whether the code uses
strlenormb_strlen). - Overflow. Counters and IDs in fixed-size integers can wrap around without an error. Pick a size with plenty of room.
Try it yourself
- In Python, try
0.1 + 0.1 + 0.1 == 0.3, thenround(0.1 + 0.1 + 0.1, 2) == 0.3. - Compare
len(s)andlen(s.encode())for your own name, a word with an accent, and an emoji. - In your browser console, type
9007199254740993and see what comes back. Then addnat the end. - If you have Go installed, make an
int8overflow, then change it toint16and see what changes.
Summary
- A bit is 0 or 1; a byte is 8 bits (256 possible values).
- Bits have no meaning on their own. The type decides how they are read.
- Fixed-size integers can wrap around; Python integers don’t; PHP turns them into floats; JavaScript numbers are exact only up to 2⁵³ − 1.
- Floating point can’t represent most decimal fractions exactly, so
0.1 + 0.2isn’t0.3. Don’t use floats for money. - Text is stored through an encoding. UTF-8 uses 1 to 4 bytes per character, so “length” depends on what you count.
References
- Floating-Point Arithmetic: Issues and Limitations, Python tutorial.
- Built-in Types: numeric types, Python documentation.
- Number.MAX_SAFE_INTEGER and String: length, MDN.
- F. Yergeau, RFC 3629: UTF-8, a transformation format of ISO 10646, November 2003.
- ASCII, Wikipedia.
- Integers, Floating-point numbers, and strlen, PHP Manual.