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 digit is a bit. Unlike decimal, where each place is worth ten times the place to its right, each binary place is worth twice as much. section 1.3

Reading from right to left, the first eight places have values 1, 2, 4, 8, 16, 32, 64, and 128. A 1 includes that place’s value; a 0 leaves it out. Add the included values to find the decimal number. section 1.3

The largest eight-bit unsigned value

The binary number 11111111 has a 1 in every place, so its value is 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255. That is the largest value Rust’s u8 type can hold. section 1.3

More bits provide more places and allow larger values. Eight bits make one byte. Rust’s u16 has 16 bits and can hold up to 65,535; understanding these limits helps when choosing an integer type. section 1.3 section 1.12

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains bit values and unsigned maximums through doubling and addition.
Examples
Works through 11111111 as 255 and compares the maximums of u8 and u16.
What you can do after
Evaluate which integer type fits a value by considering its bit width and range.
How it connects
Connects binary representation to bits, bytes, and signed and unsigned integers.

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.