0
0
Cprogramming~5 mins

Bit manipulation techniques - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AAND (&) with the complement of (1 << n)
BOR (|) with (1 << n)
CXOR (^) with (1 << n)
DLeft shift (<<) by n
What is the result of 5 & 3 in binary?
A7
B6
C1
D0
How do you toggle the 2nd bit of a number?
Anumber ^= (1 << 2);
Bnumber &= ~(1 << 2);
Cnumber |= (1 << 2);
Dnumber << 2;
What does the expression (number >> 1) do?
ASets the 1st bit of number
BShifts bits of number to the right by 1, dividing by 2
CShifts bits of number to the left by 1, multiplying by 2
DClears the 1st bit of number
Which operator can be used to check if a number is even or odd?
ALeft shift (<<) by 1
BOR (|) with 1
CXOR (^) with 1
DAND (&) with 1
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.