Determine the number of bits required to flip if you want to convert integer n to integer m.

Notice

Both n and m are 32-bit integers.

Example

Given n = 31 (11111), m = 14 (01110), return 2.

 class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int diff = a ^ b;
int count = ;
while (diff != ) {
count++;
// remove the last 1
diff = diff & (diff - ); // 这种方法很好。
}
return count;
}
};

Flip Bits的更多相关文章

  1. 181. Flip Bits【easy】

    181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n  ...

  2. LintCode刷题笔记--Flip Bits

    Flip Bits: 标签:位运算 题目:Determine the number of bits required to flip if you want to convert integer n  ...

  3. Lintcode: Flip Bits

    Determine the number of bits required to flip if you want to convert integer n to integer m. Have yo ...

  4. 181. Flip Bits【LintCode, by java】

    Description Determine the number of bits required to flip if you want to convert integer n to intege ...

  5. lintcode:Flip Bits 将整数A转换为B

    题目: 将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 样例 如把31转换为14,需要改变2个bit位. ()10=()2 ()10=()2 挑战 你能想出几种方法? 解题: A- ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. c++ bitset使用

    A bitset is a special container class that is designed to store bits (elements with only two possibl ...

  8. 黑科技--位集--bitset

    自从上次网赛发现这么个东西之后,深深地感受到了bitset的强大,0.0. 正常的bool占用1字节空间,bitset可以把这个缩到1bit,空间上8倍优化.正常用起来可能会跟位运算状态压缩类似,但是 ...

  9. C++ std::vector<bool>

    std::vector template < class T, class Alloc = allocator<T> > class vector; // generic te ...

随机推荐

  1. service依赖dao的接口进行数据传输

    service依赖dao的接口进行数据传输

  2. c语言基本数据类型(short、int、long、char、float、double)

    一 C 语言包含的数据类型 short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 在不同的系统上,这些类型占据的字节长度是不同的: 在32 ...

  3. Graph And Its Complement CodeForces - 990D(思维构造)

    题意: 图中有n个点,开始有a个连通块,然后连着的边断开,不连的边连上,变为b个连通块,输出原图的邻接矩阵. 解析: 原图中连通块大于1的图,经过上述操作后,一定变成只有1个连通块的图. 若n != ...

  4. Hihocoder之conv2d()

    http://hihocoder.com/contest/tupu2018/problem/2 题目2 : ​Standard 2D Convolution 时间限制:5000ms 单点时限:1000 ...

  5. python3.6关键字总结

    模块是个好东西 import keyword # 导入关键字模块 lst = keyword.kwlist # 实例化 print(lst) # 看看有哪些玩意 print(len(lst)) # 貌 ...

  6. 2:spring中的@resource

    @Resource 其实是spring里面的注解注入. @Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定, 如果没有指定name属性,当注解写在字段 ...

  7. centos 安装MATLAB :设置回环设备失败: 没有那个文件或目录

    基本参数:centos 7 x86_64,linux 系统, 安装matlab, 已经下载R2016b_glnxa64.iso 但挂载的时候遇到问题: [root@lf mnt]# mount -o  ...

  8. C/C++ 多继承{虚基类,虚继承,构造顺序,析构顺序}

    C/C++:一个基类继承和多个基类继承的区别 1.对多个基类继承会出现类之间嵌套时出现的同名问题,如果同名变量或者函数出现不在同一层次,则底层派生隐藏外层比如继承基类的同名变量和函数,不会出现二义性, ...

  9. laravel DB listen 回调追踪产生 sql 语句的代码

    \DB::listen(function (QueryExecuted $sql) { \Log::info($sql->sql); \Log::info((new \Exception())- ...

  10. 数据中有NA存在,处理办法

    如果数据中有NA存在,表示这个位置数据遗失,不能进行值的类型描述.也不能用函数来计算,需要计算是可以加上na.rm=T表示忽略NA,但是这个位置并没有去除,使用length可以看到. > x&l ...