Counting number of bits How does this line work ? nnn-1;
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In the world of computer science, bit manipulation is a powerful tool for optimizing algorithms and processing datasets efficiently. One classic problem tackled with bit manipulation is counting the number of 1-bits (or "set bits") in an integer's binary representation. A well-known and efficient approach to solve this problem involves using the line of code `n = n & (n - 1);`. This operation repeatedly reduces the number of set bits in `n` until `n` becomes zero. Let's delve into how this line works and explore some technical explanations and examples to consolidate our understanding.
How the Line `n = n & (n - 1);` Works
Technical Explanation
When using `n = n & (n - 1);`, the objective is to clear the least significant set bit of the number `n` with each iteration. The bitwise AND operation between `n` and `n-1` is the key here.
- Binary Representation: To understand the operation, let's first look at how binary subtraction and bitwise AND work:
- Assume `n` in binary is represented as `...1000`.
- Now, `n-1` would then be `...0111`.
- Effect of Subtraction by 1:
- Subtracting `1` from `n` flips all bits to the right of the least significant set bit, including the set bit itself.
- Therefore, the least significant set bit is changed and any 0-bits become 1's.
- AND Operation:
- Performing the bitwise AND (`&`) between `n` and `n-1`, you get a result where the least significant set bit of `n` is cleared, and all bits to the right remain 0.
Example
Let's illustrate this with an example using the decimal number 12:
- Step-by-step Execution for `n = n & (n - 1);`:
- `n = 12` in binary is `1100`.
- `n - 1` is `11` in binary, which is `1011`.
- `n & (n - 1)` results in `1100 & 1011`:
- Repeating this step reduces `n` further:
- Time Complexity: The algorithm runs in where is the number of set bits. This is efficient, especially for numbers with a relatively small number of bits set compared to their overall size.
- Space Complexity: The solution requires additional space as it doesn't rely on storage beyond a few variables.
- Cryptography: Counting set bits helps in calculations over binary data, crucial in cryptographic algorithms.
- Digital Image Processing: Analyzing image data often involves bit manipulations to enhance, compress, or recognize patterns effectively.
- Network Protocols: Enabling efficient bandwidth usage by managing data packet headers with bitwise operations.
Related reading
- Counting palindromic substrings in On
- Counting the adjacent swaps required to convert one permutation into another
- Counting the bits set in the Fibonacci number system?
- Course assignment algorithm
- Create 2d triangles from 2d points
- Create a hashcode of two numbers
- Create a random permutation of 1..N in constant space
- Create Balanced Binary Search Tree from Sorted linked list

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.