设为首页 加入收藏

TOP

UC校园招聘C++工程师笔试题
2014-11-24 01:34:56 来源: 作者: 【 】 浏览:9
Tags:校园招聘 工程师 试题

要求根据代码分析代码的功能:
unsigned int CountOne(unsigned int x){
x = (x & 0×55555555) + (x >> 1 & 0×55555555);
x = (x & 0×33333333) + (x >> 2 & 0×33333333);
x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f);
x = (x & 0x00ff00ff) + (x >> 8 & 0x00ff00ff);
x = (x & 0x0000ffff) + (x >> 16 & 0x0000ffff);
return x;
}
就这么多。
经过检验,这是检验的代码:
#include
using namespace std;
unsigned int CountOne(unsigned int x);
int main(){
unsigned int x;
cout<<”Input a num and will return the count of one:”< cout<<”(like input 211,and result:5)”< cin>>x;
cout<<”result:”< cout< return 0;
}
unsigned int CountOne(unsigned int x){
x = (x & 0×55555555) + (x >> 1 & 0×55555555);
x = (x & 0×33333333) + (x >> 2 & 0×33333333);
x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f);
x = (x & 0x00ff00ff) + (x >> 8 & 0x00ff00ff);
x = (x & 0x0000ffff) + (x >> 16 & 0x0000ffff);
return x;
}
该函数就是计算一个32位无符号整数中含有1的个数,就5行代码。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇一个三层的应用程序通常有:表现.. 下一篇.NET面试题大全 初级程序员 中级..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: