题目:将整数A转换为B
2015-08-19 19:17
155 查看
如果要将整数A转换为B,需要改变多少个bit位?
您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?
Airbnb
Alibaba
Amazon Apple
Baidu Bloomberg
Cisco Dropbox
Ebay Facebook
Google Hulu
Intel Linkedin
Microsoft NetEase
Nvidia Oracle
Pinterest Snapchat
Tencent Twitter
Uber Xiaomi
Yahoo Yelp
Zenefits
感谢您的反馈
样例
如把31转换为14,需要改变2个bit位。
(31)10=(11111)2
(14)10=(01110)2
挑战
你能想出几种方法?
标签 Expand
Cracking The Coding Interview
比特位操作
class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int count = 0;
int x = a^b;
while(x!=0){
if((x&1)==1){
count++;
}
x = x>>>1;
}
return count;
}
};
您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?
Airbnb
Alibaba
Amazon Apple
Baidu Bloomberg
Cisco Dropbox
Ebay Facebook
Google Hulu
Intel Linkedin
Microsoft NetEase
Nvidia Oracle
Pinterest Snapchat
Tencent Twitter
Uber Xiaomi
Yahoo Yelp
Zenefits
感谢您的反馈
样例
如把31转换为14,需要改变2个bit位。
(31)10=(11111)2
(14)10=(01110)2
挑战
你能想出几种方法?
标签 Expand
Cracking The Coding Interview
比特位操作
class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int count = 0;
int x = a^b;
while(x!=0){
if((x&1)==1){
count++;
}
x = x>>>1;
}
return count;
}
};
相关文章推荐
- oracle游标:查询并打印员工的姓名和薪水
- iOS中Storyboard使用要点记录
- EF里Guid类型数据的自增长、时间戳和复杂类型的用法
- 题目:字符串查找
- NIO(JDK1.4)--选择器Selector
- 阿里云server安全设定
- 时间戳
- OC中nil、Nil、NULL、NSNull的区别
- android开发 解决启动页空白或黑屏问题
- iOS面试题三
- libcoubase源码解析
- 多线程
- webx turbine 前端开发入门理解
- 汇编中精妙的流程控制
- SQL
- Swift开发教程--如何给UITextView添加圆角边框
- 返享微信“卖”宝马,暴露朋友圈营销本质
- Maximum Gap
- 题目:子树
- VC++如何创建无CMD窗口控制台程序