博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Count total set bits in all numbers from 1 to n
阅读量:4150 次
发布时间:2019-05-25

本文共 2240 字,大约阅读时间需要 7 分钟。

reference: 

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<

转载地址:http://krxti.baihongyu.com/

你可能感兴趣的文章
rootkit related
查看>>
配置文件的重要性------轻化操作
查看>>
又是缓存惹的祸!!!
查看>>
为什么要实现程序指令和程序数据的分离?
查看>>
我对C++ string和length方法的一个长期误解------从protobuf序列化说起(没处理好会引起数据丢失、反序列化失败哦!)
查看>>
一起来看看protobuf中容易引起bug的一个细节
查看>>
无protobuf协议情况下的反序列化------貌似无解, 其实有解!
查看>>
make -n(仅列出命令, 但不会执行)用于调试makefile
查看>>
makefile中“-“符号的使用
查看>>
go语言如何从终端逐行读取数据?------用bufio包
查看>>
go的值类型和引用类型------重要的概念
查看>>
求二叉树中结点的最大值(所有结点的值都是正整数)
查看>>
用go的flag包来解析命令行参数
查看>>
来玩下go的http get
查看>>
队列和栈的本质区别
查看>>
matlab中inline的用法
查看>>
如何用matlab求函数的最值?
查看>>
Git从入门到放弃
查看>>
java8采用stream对集合的常用操作
查看>>
EasySwift/YXJOnePixelLine 极其方便的画出真正的一个像素的线
查看>>