0
0
Cprogramming~10 mins

Bit manipulation techniques - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the 3rd bit of the variable num.

C
num = num | (1 << [1]);
Drag options to blanks, or click blank then click option'
A3
B0
C2
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 instead of 2 for the bit position.
Confusing bit positions starting from 1 instead of 0.
2fill in blank
medium

Complete the code to clear (set to 0) the 5th bit of num.

C
num = num & ~(1 << [1]);
Drag options to blanks, or click blank then click option'
A4
B5
C3
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 instead of 4 for the bit position.
Forgetting to negate the mask before AND operation.
3fill in blank
hard

Fix the error in the code to toggle the 1st bit of num.

C
num = num [1] (1 << 0);
Drag options to blanks, or click blank then click option'
A|
B^
C&
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using AND or OR instead of XOR for toggling.
Using right shift operator which does not toggle bits.
4fill in blank
hard

Fill both blanks to check if the 4th bit of num is set (1).

C
if ((num [1] (1 << [2])) != 0) {
    // bit is set
}
Drag options to blanks, or click blank then click option'
A&
B|
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR instead of AND for checking bits.
Using wrong bit position like 3 instead of 4.
5fill in blank
hard

Fill all three blanks to create a mask that keeps only the lower 3 bits of num.

C
mask = (1 << [1]) [2] 1;
result = num [3] mask;
Drag options to blanks, or click blank then click option'
A3
B-
C&
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR instead of AND to apply the mask.
Using addition instead of subtraction in mask creation.
Using wrong bit count for shifting.