LintCode Flip Bits
2015-09-13 22:46
357 查看
Determine the number of bits required to flip if you want to convert integer n to integer m.
Have you met this question in a real interview? Yes
Example
Given n = 31 (11111), m = 14 (01110), return 2.
Note
Both n and m are 32-bit integers.
class Solution { public: /** *@param a, b: Two integer *return: An integer */ int bitSwapRequired(int a, int b) { // write your code here int diff = a ^ b; int count = 0; while (diff) { count++; diff = diff & (diff - 1); } return count; } };
相关文章推荐
- docker常用命令
- Linux下可执行文件格式详解
- 05字符串格式化符号含义以及转义符号含义
- XFire发布Web Services
- 苹果开发者帐号三种类型的区别
- Sublime Text2使用教程
- axure & Markman学习总结
- LeetCode -- Add Digits
- Java中的反射机制
- android调用手机相机、相册,设置头像
- 快速生成findviewbyid
- js用addeventlistener绑定事件,阻止默认事件。
- 【poj1331】确定进制
- 堆与栈的区别
- HTML&CSS Learning Notes 2 (Additional)
- HDU 5444 Elven Postman 解题报告(水题)
- Java记录 -31- Java数组
- Struts2配置
- ThinkPHP使用Memcached
- python操作数据库 - SQLAlchemy