Given a positive integer N, the task is to count the total number of unset bits in the binary representation of all the numbers from 1 to N. Note that leading zeroes will not be counted as unset bits.
Input: N = 5
Integer Binary Representation Count of unset bits 1 1 0 2 10 1 3 11 0 4 100 2 5 101 1
0 + 1 + 0 + 2 + 1 = 4
Input: N = 14
- Iterate the loop from 1 to N.
- While number is greater than 0 divide it by 2 and check the remainder.
- If remainder is equal to 0 then increase the value of count by 1.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Count unset bits of a number
- Count unset bits in a range
- Python | Count unset bits in a range
- Count total set bits in all numbers from 1 to n
- Python map function | Count total set bits in all numbers from 1 to n
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to N | Set 3
- Count total set bits in all numbers from range L to R
- Find the largest number with n set and m unset bits
- Unset bits in the given range
- Find the smallest number with n set and m unset bits
- Unset the last m bits
- Check if a number has same number of set and unset bits
- Unset least significant K bits of a given number
- Count of numbers having only one unset bit in a range [L,R]
- Count of total bits toggled/flipped in binary representation of 0 to N
- Count total bits in a number
- Total ways of choosing X men and Y women from a total of M men and W women
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.