Use a bitwise test to report whether seven is odd.
Acceptance criteria
- 1.#out should show odd.
DSA Tutorial · Bit Manipulation
These 10 tasks go with the Bit Manipulation Basics chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Bit tricks are rarely the right tool in application code and are asked about constantly in interviews. Learn the handful that come up; resist using them where a boolean would read better.” .
1.Test odd numbersexercise · easy · 10 min
Use a bitwise test to report whether seven is odd.
Passes when: #out should show odd.
2.Halve with a shiftexercise · easy · 10 min · practice plan
Halve thirteen using a shift and show the result.
Passes when: #out should show half 6.
3.Count set bitsexercise · easy · 10 min · practice plan
Count the one bits in thirteen.
Passes when: #out should show bits 3.
4.Find the lonely valueexercise · easy · 10 min · practice plan
Every value appears twice except one — find it with XOR.
Passes when: #out should show alone 4.
5.Power of twoexercise · medium · 10 min · practice plan
Report whether sixteen is a power of two.
Passes when: #out should show power of two.
6.Fix the odd test using modulo on a negativebug fix · easy · 8 min
The remainder of a negative number is negative, so the equality fails.
Passes when: #out should show odd.
7.Fix the shift in the wrong directionbug fix · easy · 8 min · practice plan
Shifting left doubles the value rather than halving it.
Passes when: #out should show half 6.
8.Fix the bit count clearing the wrong bitbug fix · medium · 8 min · practice plan
Adding one instead of subtracting never clears a bit, so a guard stops it.
Passes when: #out should show bits 3.
9.Fix the lonely value found with additionbug fix · easy · 8 min · practice plan
Summing does not cancel the pairs the way XOR does.
Passes when: #out should show alone 4.
10.Fix the 32-bit overflowbug fix · easy · 8 min · practice plan
Bitwise operators truncate to 32 bits, so shifting by 32 wraps back to one.
Passes when: #out should show exact.
Between them these tasks cover Masks, XOR and 32-bit limits.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.