Bit Manipulation Essentials

easy
Unit 6 · Matrices0/3 to milestone

Implement these with bitwise operators:

  • single_number(nums): every value appears twice except one. Find it in O(1) memory (XOR).
  • count_bits(n): a list where entry i is the number of 1-bits in i, for 0 ≤ i ≤ n, using the recurrence bits[i] = bits[i >> 1] + (i & 1).
  • is_power_of_two(x): True for 1, 2, 4, 8, …, and False for 0 and negatives.
  • missing_number(nums): nums holds n distinct values from 0..n with one missing. Find it with XOR.
  • reverse_bits(x): reverse the bits of a 32-bit unsigned integer.
#bits#math2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Bit Manipulation Essentials · TechGradify