liveBook · Manning

U16

As explained in Rust in Action: Systems programming concepts and techniques

u16 is Rust's unsigned 16-bit integer type, representing values from 0 through 65,535.

`u16` is Rust’s unsigned 16-bit integer type. It holds whole numbers from 0 through 65,535, inclusive. Its 16 bits provide a fixed set of patterns: all zeros represent 0, and all ones represent 65,535. section 5.2

A calculation whose result falls outside that range causes integer overflow. For example, 65,536 cannot fit in a `u16`. In the incrementing example, compiling with default `rustc` options produces a panic; compiling with `rustc -O` gives different behavior. section 5.2

Listing 5.3 Exploring the effect of incrementing an integer past its range
 fn main() {
   let mut i: u16 = 0;
   print!("{}..", i);
 
   loop {
       i += 1000;
       print!("{}..", i);
       if i % 10000 == 0 {
           print!{"\n"}
       }
   }
 }

Follow a `u16` counter as it increases by 1,000 on each iteration. Eventually the next result cannot fit in the type. section 5.2

A `u16` can also hold a CHIP-8 opcode. Each opcode has four 4-bit nibbles, which can be extracted with bitwise AND and right shifts. Some fields combine nibbles into 8-bit or 12-bit values. section 5.7.1

Listing 5.21 Extracting variables from an opcode
fn main() {
  let opcode: u16 = 0x71E4;
 
  let c = (opcode & 0xF000) >> 12;     #1
  let x = (opcode & 0x0F00) >>  8;     #1
  let y = (opcode & 0x00F0) >>  4;     #1
  let d = (opcode & 0x000F) >>  0;     #1
 
  assert_eq!(c, 0x7);                  #2
  assert_eq!(x, 0x1);                  #2
  assert_eq!(y, 0xE);                  #2
  assert_eq!(d, 0x4);                  #2
 
  let nnn = opcode & 0x0FFF;           #3
  let kk  = opcode & 0x00FF;           #3
 
  assert_eq!(nnn, 0x1E4);
  assert_eq!(kk,   0xE4);
}

For opcode `0x71E4`, masks and shifts extract the four nibbles `7`, `1`, `E`, and `4`. Other masks select the low 12 or 8 bits. section 5.7.1

When an opcode is stored as two `u8` values in memory, cast each byte to `u16`, shift the first left by eight bits, and combine it with the second using bitwise OR. section 5.7.3

Listing 5.24 Reading an opcode from memory
 8 fn read_opcode(&self) -> u16 {
 9   let p = self.position_in_memory;
10   let op_byte1 = self.memory[p] as u16;
11   let op_byte2 = self.memory[p + 1] as u16;
12  
13   op_byte1 << 8 | op_byte2       #1
14 }

This `read_opcode` method assembles two memory bytes into one `u16`. section 5.7.3

An array of opcodes

Three instructions can be stored as a `[u16; 3]` array: `0x8014`, `0x8014`, and `0x00EE`. To load them into byte-oriented memory, the example expresses them as six `u8` values: `0x80`, `0x14`, `0x80`, `0x14`, `0x00`, and `0xEE`. section 5.7.4

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains overflow and bit patterns, then works through opcode extraction and assembly from bytes.
Examples
Uses an incrementing counter, opcode 0x71E4, and a three-instruction array decomposed into bytes.
What it assumes
Builds on earlier introductions to integer types, bitwise AND, and right shifts.
What you can do after
Understand how to recognize u16 overflow and extract opcode fields with masks and shifts.
How it connects
Connects u16 to integer overflow, bitwise operations, opcodes, memory, and CPU emulation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.