问题描述
给定L,R。统计[L,R]区间内的所有数在二进制下包含的“1”的个数之和。
如5的二进制为101,包含2个“1”。
输入格式
第一行包含2个数L,R
输出格式
一个数S,表示[L,R]区间内的所有数在二进制下包含的“1”的个数之和。
样例输入
2 3
样例输出
3
数据规模和约定
L<=R<=100000;
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
int L,R;
cin >> L >> R;
int ans=0;
for(int i=L;i<=R;i++){
int t=i;
while(t){
ans+=t%2;
//ans+=(int)(t&1);
t=t/2;
//t >>=1;
}
}
cout << ans;
}