LearnFrenzy
Sign in Take a free test

Bitwise Operators · Exercise 4 of 4

Bitwise Operators Bit Manipulation Programs

  • 8 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Classic bit-manipulation interview problems. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Easy

Write four small functions to set, clear, toggle and test bit k of an unsigned value, and try them on 42.

Question page & discussion
Question 2 Easy

Swap the high and low 4 bits of a byte, e.g. 0xAB becomes 0xBA.

Question page & discussion
Question 3 Medium

Every number appears twice except one. Find it in O(n) time and O(1) space.

Question page & discussion
Question 4 Medium

Count how many bits must change to turn one number into another (the Hamming distance).

Question page & discussion
Question 5 Hard

Rotate a uint32_t left by n bits, so bits that fall off the top come back at the bottom. It must be safe for n = 0 and n ≥ 32.

Question page & discussion
Question 6 Hard

Find the smallest power of two that is ≥ n, without loops over every bit.

Question page & discussion
Question 7 Medium

Print every subset of {a, b, c} using the bits of a counter.

Question page & discussion
Question 8 Easy

Check whether two integers have opposite signs with a single bitwise expression.

Question page & discussion
All exercises doneBack to Bitwise OperatorsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links