Complete the code to set the 3rd bit of number n.
n = n | (1 << [1]);
Bits are zero-indexed from the right. The 3rd bit means index 2, so shifting 1 by 2 sets the 3rd bit.
Complete the code to clear the 4th bit of number n.
n = n & ~(1 << [1]);
Clearing the 4th bit means zeroing bit at index 3 (zero-based). So shift 1 by 3 and invert.
Fix the error in the code to toggle the 5th bit of number n.
n = n [1] (1 << 4);
Toggling a bit uses XOR (^). XOR flips the bit if it is 1, else sets it if 0.
Fill both blanks to check if the 2nd bit of n is set and print "Yes" if true.
if (n [1] (1 << [2])) { std::cout << "Yes" << std::endl; }
To check if a bit is set, use bitwise AND (&) with 1 shifted by the bit index. The 2nd bit is index 1.
Fill all three blanks to set the 1st bit, clear the 3rd bit of n.
n = n [1] (1 << 0); n = n [2] ~(1 << [3]);
Set 1st bit with OR (|) and shift 0. Clear 3rd bit with AND (&) and inverted mask shifted by 2.
