/**
* #959 Daily Coding Problem: Problem [Easy]
* <p>
* Good morning! Here's your coding interview problem for today.
* This problem was asked by Pivotal.
* Write an algorithm that finds the total number of set bits in all integers between 1 and N.
*/publicclassSetNitsInAllIntegers{privatestaticintcountSetBits(intn){intcount=0;while(n>0){// n对1做位与运算,可以得出最后一位是1或者是0
count+=n&1;// n 右移一位
n>>=1;}returncount;}publicstaticvoidmain(String[]args){for(inti=0;i<10;i++){intv=countSetBits(i);System.out.println(i+" count set bits is : "+v);}}}