Posted on by Kalkicode
Code Bit Logic

# Count total unset bits in all numbers from 1 to n

The problem we're addressing is to count the total number of unset bits (also known as inactive bits or 0s) in the binary representation of all numbers from 1 to a given positive integer `n`. This problem involves calculating the sum of unset bits in the binary representation of each number within the range.

## Problem Statement and Description

Given a positive integer `n`, the task is to count the total number of unset bits (0s) in the binary representation of all numbers from 1 to `n`. In other words, we need to sum up the number of 0s in the binary representation of each number within the range [1, n].

## Example

Consider the number 7. The binary representations of the numbers from 1 to 7 are as follows:

• 1: 1 (0 unset bits)
• 2: 10 (1 unset bits)
• 3: 11 (0 unset bits)
• 4: 100 (2 unset bits)
• 5: 101 (1 unset bits)
• 6: 110 (1 unset bits)
• 7: 111 (0 unset bits)

The total number of unset bits in these binary representations is 5. Therefore, the expected output for this example should be "Unset bits from (1 to 7): 5".

## Idea to Solve the Problem

The main idea to solve this problem is to utilize the properties of unset bits in the binary representation. We can use the `countSetBit` function (which you provided) to calculate the number of set bits in a single number. Then, we can calculate the number of unset bits by subtracting the count of set bits from the total number of bits in the binary representation.

## Pseudocode

Here's the pseudocode for the algorithm:

``````function countSetBit(num):
// Count set bit
function countUnsetBit(n):
result = 0
for i from 1 to n:
total_bits = log(i) / log(2.0) + 1.0
result += total_bits - countSetBit(i)
print "Unset bits from (1 to ", n, "): ", result``````

## Algorithm Explanation

1. `countSetBit(num)` calculates the number of set bits (active bits) in the binary representation of a given number using your provided implementation.
2. `countUnsetBit(n)` initializes a variable `result` to store the total count of unset bits.
3. It then iterates through the numbers from 1 to `n` using a loop.
4. For each number `i`, it calculates the total number of bits in its binary representation by using the formula `log(i) / log(2.0) + 1.0`.
5. It subtracts the count of set bits in the binary representation of `i` from the total number of bits to get the count of unset bits.
6. After the loop completes, the `result` variable will contain the total number of unset bits in the binary representations of all numbers from 1 to `n`.
7. Print the results.

## Time Complexity

The time complexity of the algorithm primarily depends on the iteration through the numbers from 1 to `n` and the calculation of unset bits using the `countSetBit` function and the logarithmic operation. Since these operations are performed sequentially, the overall time complexity can be approximated as O(n * log n), where `n` is the input number.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post