fn main() {
let a: u8 = 0b10101010;
let b: u8 = 0b11001100;
let c = a & b;
let d = a | b;
println!("{:08b} {:08b}", c, d);
}Bitwise AND of 0b10101010 and 0b11001100 is 0b10001000.
Bitwise OR of the same is 0b11101110.
Printed as 8-bit binary strings with leading zeros.
fn main() {
let x: u8 = 0b11110000;
let y = !x;
let z = x ^ y;
println!("{:08b} {:08b}", y, z);
}NOT of 0b11110000 is 0b00001111.
XOR of 0b11110000 and 0b00001111 is 0b11111111.
Printed as 8-bit binary strings.
fn main() {
let n: u8 = 0b00001111;
let left = n << 2;
let right = n >> 1;
println!("{:08b} {:08b}", left, right);
}Left shift 0b00001111 by 2 bits gives 0b00111100.
Right shift 0b00001111 by 1 bit gives 0b00000111.
Printed as 8-bit binary strings.
XOR (^) sets bits to 1 only when the bits differ between operands.
AND (&) sets bits to 1 only if both bits are 1.
OR (|) sets bits to 1 if either bit is 1.
NOT (~) flips all bits of one operand.
In Rust, shifting a u8 by 8 bits or more causes a panic at runtime.
This is because the shift amount must be less than the number of bits in the type.
The code compiles but panics when run.
