liveBook · Manning

Binary representation

As explained in Learn Rust in a Month of Lunches

Binary representation expresses values using a base-two numeral system and corresponding bit patterns.

Binary representation writes numbers using just two digits: 0 and 1. Each position is a bit. Unlike decimal positions, which increase by factors of ten, binary positions increase by factors of two. section 1.3

Read a bit pattern from right to left: its positions are worth 1, 2, 4, 8, and so on. A 1 includes that position’s value; a 0 leaves it out. Add the included values to find the number. section 1.3

Why a u8 stops at 255

A u8 has eight bits. With every bit set to 1, its pattern is 11111111, worth 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255. A u16 has eight additional bits, so it can hold values up to 65,535. More bits mean room for larger numbers. section 1.3

This gives you a practical way to think about Rust integer types: the number in a type name such as u8 or u16 tells you how many bits it has. Use that bit count to judge whether the type can hold the numbers you need. section 1.3 section 1.12

How Learn Rust in a Month of Lunches covers it

How deep it goes
Works through binary place values and eight-bit capacity; does not explain how signed values are encoded.
Examples
Adds the eight place values in 11111111 to obtain 255, then compares u8 with u16.
What it assumes
Starts with familiar decimal numbers and defines binary digits and bits without assuming knowledge of binary notation.
What you can do after
Evaluate/understand whether an integer type has enough bits for a needed value.
How it connects
Connects binary digits and bits to decimal place values, bytes, and signed and unsigned integer types.

Also explained in

Compare how 6 books explain binary representation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.