Count total set bits in all numbers from 1 to n
来源:互联网 发布:网络大型策略游戏 编辑:程序博客网 时间:2024/06/05 10:26
reference:
http://www.geeksforgeeks.org/count-total-set-bits-in-all-numbers-from-1-to-n/
Problem Definition:
Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n.
Examples:
Input: n = 3Output: 4Input: n = 6Output: 9
Solution:
If the input number is of the form 2^b -1 e.g., 1,3,7,15.. etc, the number of set bits is b * 2^(b-1). This is because for all the numbers 0 to (2^b)-1, if you complement and flip the list you end up with the same list (half the bits are on, half off).
If the number does not have all set bits, then some position m is the position of leftmost set bit. The number of set bits in that position is n – (1 << m) + 1. The remaining set bits are in two parts:
1) The bits in the (m-1) positions down to the point where the leftmost bit becomes 0, and
2) The 2^(m-1) numbers below that point, which is the closed form above.
An easy way to look at it is to consider the number 6:
0|0 00|0 10|1 00|1 1-|–1|0 01|0 11|1 0
The leftmost set bit is in position 2 (positions are considered starting from 0). If we mask that off what remains is 2 (the "1 0" in the right part of the last row.) So the number of bits in the 2nd position (the lower left box) is 3 (that is, 2 + 1). The set bits from 0-3 (the upper right box above) is 2*2^(2-1) = 4. The box in the lower right is the remaining bits we haven't yet counted, and is the number of set bits for all the numbers up to 2 (the value of the last entry in the lower right box) which can be figured recursively.
Code:
// Returns count of set bits present in all numbers from 1 to nunsigned int countSetBits(unsigned int n){ // Get the position of leftmost set bit in n. This will be // used as an upper bound for next set bit function int m = getLeftmostBit (n); // Use the position return _countSetBits (n, m);} unsigned int _countSetBits(unsigned int n, int m){ // Base Case: if n is 0, then set bit count is 0 if (n == 0) return 0; /* get position of next leftmost set bit */ m = getNextLeftmostBit(n, m); // If n is of the form 2^x-1, i.e., if n is like 1, 3, 7, 15, 31,.. etc, // then we are done. // Since positions are considered starting from 0, 1 is added to m if (n == ((unsigned int)1<<(m+1))-1) return (unsigned int)(m+1)*(1<<m); // update n for next recursive call n = n - (1<<m); return (n+1) + countSetBits(n) + m*(1<<(m-1));}
- Count total set bits in all numbers from 1 to n
- set all bits between i and j in N equal to M
- [CrackCode] 5.1 Set all bits between i and j in N equal to M
- Select n numbers from 1 to m with all of array 'all' and none of array 'none'
- Count set bits in an integer
- Problem 17 - How many letters would be needed to write all the numbers in words from 1 to 1000?
- 448. Find All Numbers Disappeared in an Array -- set
- Find all the narcissistic numbers from 100 to 999
- 448 Find All Numbers Disappeared in an Array找到[1,n]内没有出现的数字
- Sum of numbers from 0 to N -- 7 kyu
- LCM from 1 to n
- LCM from 1 to n
- Do you know how many combinations to select n numbers from 1 to m with sum of which is mysum?
- geeksforgeeks Count all possible walks from a source to a destination with exactly k edges
- Q5.1 Set Bits
- P24 (*) Lotto: Draw N different random numbers from the set 1..M.
- ZOJ Problem Set - 1970 All in All
- 【Leetcode】Numbers of 1 bits
- 迅雷安装脚本
- 单机搭建oracle dg 实验与总结
- SQL 中详解round(),floor(),ceiling()函数的用法和区别?
- 更改NavigationController的背景图片
- JUnit 4的参数化测试
- Count total set bits in all numbers from 1 to n
- Android 源代码在线查看
- 网页提示证书过期的时候,可能是因为系统时间不对造成的。
- 在cocos2d 1.0里面 如何描画图片的一部分. 实现j2me里面的setClip
- 暗暗
- 几个windows命令
- SQL IDENTITY(函数) 自动加自
- jquery获取时间差、时间格式的代码
- 给Emacs配色时, 比较简单解决方案