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. Python实现双色球和大乐透摇奖

    实现代码: # code by kadycui # 模块引用 import random def select(): print('\n') print('请选择彩票种类') print('双色球输入 ...

  2. [NOI2014]魔法森林 LCT

    题面 [NOI2014]魔法森林 题解 一条路径的代价为路径上的\(max(a[i]) + max(b[i])\),因为一条边同时有$a[i], b[i]$2种权值,直接处理不好同时兼顾到,所以我们考 ...

  3. 【bzoj1087】互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  4. View 的setTag() 和 getTag()

    View 的setTag() 和 getTag()ImageView.setTag(""); 就是当多个ImageView用到一个监听器时,能触发不同的情况,是View的方法 Ad ...

  5. 【bzoj3926】【Zjoi2015】诸神眷顾的幻想乡

    题解: 如果树上某个路径串的端点不是叶子,那么一定是另一个串的子串: 这样只对叶子$dfs$把$20*20$个串插入广义$SAM$就是统计本质不同的串的个数的模板了: 我不太会分析广义$SAM$的空间 ...

  6. shiro权限认证与授权

    什么是shiro? Shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 为什么要用sh ...

  7. iperf测试网络性能

      分类: LINUX 2013-06-17 18:52:21     Iperf是一个网络性能测试工具.可以测试TCP和UDP带宽质量,可以测量最大TCP带宽,具有多种参数和UDP特性,可以报告带宽 ...

  8. Python【网络编程】内置模块urllib

    from urllib import request,parseurl = 'http://www.nnzhp.cn'req = request.urlopen(url) #打开一个url,发get请 ...

  9. 在VC6/VC2005下使程序直接具有XP风格(XP Style):

    原文 首先将以下文本保存为XpStyle.mainfest(后经实践,文件名和后缀是什么都无所谓) <?xml version="1.0" encoding="UT ...

  10. python---基础知识回顾(五)(python2.7和python3.5中的编码)

    Unicode 和 UTF-8 有何区别? python基础之字符编码 以上两篇看懂即可,那下面的就不需要看了 python标准数据类型 Bytes python--数据类型bytes Python ...