Python Truthiness and Equivalence Classes: A Mathematical Perspective
Introduction Recently, I ran into a bug in Rust while I was checking if bits were set in a u64. fn is_bit_set(n: u64, k: usize) -> bool { n & (1 << k) == 1 } Let’s run it on some input and see what happens. fn is_bit_set(n: u64, k: usize) -> bool { (n & (1 << k)) == 1 } fn main() { let n = 51; for k in 0..6 { println!("{}", is_bit_set(n, k)); } } true false false false false false This makes no sense. We know the binary representation of 51 is 110011, so we expect to see: ...