栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > C++面试题库

UC校园招聘C++工程师笔试题

UC校园招聘C++工程师笔试题

要求根据代码分析代码的功能:
unsigned int CountOne(unsigned int x){
x = (x & 0x55555555) + (x >> 1 & 0x55555555);
x = (x & 0x33333333) + (x >> 2 & 0x33333333);
x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f);
x = (x & 0x00ff00ff) + (x >> 8 & 0x00ff00ff);
x = (x & 0x0000ffff) + (x >> 16 & 0x0000ffff);
return x;
}
就这么多。
经过检验,这是检验的代码:
#include<iostream>
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:”<<endl;
cout<<“(like input 211,and result:5)”<<endl;
cin>>x;
cout<<“result:”<<endl;
cout<<CountOne(x)<<endl;
return 0;
}
unsigned int CountOne(unsigned int x){
x = (x & 0x55555555) + (x >> 1 & 0x55555555);
x = (x & 0x33333333) + (x >> 2 & 0x33333333);
x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f);
x = (x & 0x00ff00ff) + (x >> 8 & 0x00ff00ff);
x = (x & 0x0000ffff) + (x >> 16 & 0x0000ffff);
return x;
}
该函数就是计算一个32位无符号整数中含有1的个数,就5行代码。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/265671.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号