Embercode
✕
C++ OperatorsQuick quiz

Bit masks and popcount

8 questions. Covers Popcount and n & (n - 1).

Questions 1 to 5 of 8

  1. 1. Predict the output of this program. ``cpp #include <iostream> using namespace std; int main() { unsigned x = 0b1011; cout << (x & (1u << 1)) << ' ' << ((x & (1u << 2)) != 0) << endl; return 0; } ``
  2. 2. Fill in the blank: the mask with only bit 5 on is written 1u << ___.

    Type your answer.

  3. 3. A 32-bit unsigned value has k bits set. In Big-O terms, how many times does the loop while (n != 0) n &= n - 1; run?
  4. 4. Predict the output of this program. ``cpp #include <iostream> using namespace std; int main() { unsigned n = 40; n &= n - 1; cout << n << ' '; n &= n - 1; cout << n << endl; return 0; } ``
  5. 5. Which expression tests whether bit 2 of x is on?

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.