How to toggle a bit at specific position in Rust

1 Answer

0 votes
fn get_bits(n: u32) -> String {
    format!("{:b}", n)
}

fn main() {
    let mut n: u32 = 365;
    let pos: u32 = 2;

    println!("{}", get_bits(n));

    n ^= 1 << pos;

    println!("{}", get_bits(n));
}


/*
run:
     
101101101
101101001
      
*/

 



answered Apr 3 by avibootz
...