Recall & Review
beginner
What does the bitwise AND operator (&) do in C?
It compares each bit of two numbers and returns 1 only if both bits are 1; otherwise, it returns 0.
Click to reveal answer
beginner
How do you set (turn on) the nth bit of a number using bit manipulation?
Use the OR operator (|) with a mask that has only the nth bit set: <br>
number |= (1 << n);Click to reveal answer
intermediate
What is the purpose of the bitwise XOR operator (^) in bit manipulation?
It returns 1 for bits that are different and 0 for bits that are the same. It can be used to toggle bits.
Click to reveal answer
beginner
How can you check if the nth bit of a number is set (1)?
Use the AND operator with a mask and check if the result is not zero: <br>
if (number & (1 << n)) { /* nth bit is set */ }Click to reveal answer
beginner
What does the left shift operator (<<) do in bit manipulation?
It shifts all bits in a number to the left by a specified number of positions, adding zeros on the right. This multiplies the number by 2 for each shift.
Click to reveal answer
Which operator is used to clear (turn off) the nth bit of a number in C?
✗ Incorrect
To clear the nth bit, use AND with the mask that has all bits set except the nth bit: number &= ~(1 << n);
What is the result of 5 & 3 in binary?
✗ Incorrect
5 in binary is 0101, 3 is 0011; AND gives 0001 which is 1.
How do you toggle the 2nd bit of a number?
✗ Incorrect
XOR (^) with (1 << 2) flips the 2nd bit.
What does the expression (number >> 1) do?
✗ Incorrect
Right shift by 1 moves bits right, effectively dividing by 2.
Which operator can be used to check if a number is even or odd?
✗ Incorrect
Checking number & 1 returns 0 if even, 1 if odd.
Explain how to set, clear, and toggle a specific bit in a number using bit manipulation.
Think about how masks with a single bit set can change bits using AND, OR, XOR.
You got /3 concepts.
Describe how left shift and right shift operators affect a number's value and bits.
Consider how shifting bits changes the number in binary.
You got /3 concepts.