Skip to main content
Questions Why Us Pricing Log In Sign Up

Bit Manipulation: Counting 1's

How do we count the number of set bits (1-bits) in an integer?

Answer
Python
count = 0
for i in range(32):
    if (n >> i) & 1:
        count += 1

This works by examining all 32 bits of the integer and incrementing the count whenever a bit is set.

← Back to collection
← Previous question Sliding Window: Fixed-Size Template Next question Math and Logic: Missing Number →

Social

  • Instagram

Contact

  • linkedin.com/in/rinorbirinxhiku/

Legal

  • Privacy Policy
  • Terms of Service
© 2026 quant.rb. All rights reserved.