I am able to replace the value with 1 but it is not replacing with 0. Output of below program is 0b11111111
. Ideally it should be 0b11110000
. Please let me know what I am missing here.
use std::mem;
fn main() {
let a: u8 = 0b00001111;
let b: u8 = 0b11111111;
let c: u8 = 0b00000000;
let length = mem::size_of_val(&a) * 8;
let mut p : u8 = b;
for n in 0..length {
if (a >> n & 1) == 0b1 {
let d = c >> n & 1;
p = p | (d << n);
}
}
println!("0b{:08b}", p);
}