An encoding maps information to a defined sequence of symbols or bits. The same bit pattern can represent different values because the data type determines how it is interpreted, not the CPU alone. section 5.1
Text files are binary files that follow a consistent mapping between bit strings and characters. Arbitrary binary files remain opaque because they do not describe their meaning externally. section 5.1
One bit pattern, two numbers
Rust prints 1100001111000011 for both a u16 value of 50115 and an i16 value of -15421. The bits stay unchanged; the type supplies the interpretation.
Reinterpreting a float
A Rust program uses unsafe std::mem::transmute to view the bits of 42.42 as a u32, prints the 32-bit pattern, then converts it back and verifies equality. section 5.1
Instructions are encoded as numbers too, so operations and data share the same general representation. A software CPU can therefore emulate another computer’s instruction set; a CHIP-8 implementation programs directly in hexadecimal while exposing functions, pointers, and the stack. section 5.7
Listing 5.2 Interpreting a float’s bit string as an integer
fn main() {
let a: f32 = 42.42;
let frankentype: u32 = unsafe {
std::mem::transmute(a) #1
};
println!("{}", frankentype); #2
println!("{:032b}", frankentype); #3
let b: f32 = unsafe {
std::mem::transmute(frankentype)
};
println!("{}", b);
assert_eq!(a, b); #4
}
Inspect the complete Rust example that reinterprets a float’s bit string as an integer and converts it back. section 5.1








