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. HUAS 2017暑假第六周比赛-题解

    A.Parenthesis 括号匹配的问题有一种经典的做法. 将左括号看成1,右括号看成-1,做一遍前缀和sum. 括号序列是合法的当且仅当\(sum[n]=Min(sum[1],sum[2].... ...

  2. TortoiseSVN使用svn+ssh协议连接服务器时重复提示输入密码

    当使用svn+ssh协议连接svn服务器时,ssh会提示请求认证,由于不是svn客户端程序来完成ssh的认证,所以不会缓存密码. 而svn客户端通常会建立多个版本库的连接,当密码没有缓存的时候,就会重 ...

  3. 【刷题】BZOJ 2816 [ZJOI2012]网络

    Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf Solution 维护树上联通块的信息,支持动态加边删边 LCT 总共 ...

  4. BZOJ 2745: [HEOI2012]Bridge

    2745: [HEOI2012]Bridge Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 199  Solved: 90[Submit][Statu ...

  5. metasploit出错信息:can't allocate memory

    出现不能分配内存的原因: 1.postgresql服务未启动 启动服务 service postgresql start 2.虚拟机内存分配过小,如:512M 将kali虚拟机的内存扩展到1G 出错图 ...

  6. linux内核分析 第八周 理解进程调度时机跟踪分析进程调度与进程切换的过程

    笔记: 实验:使用gdb跟踪分析一个schedule()函数

  7. RabbitMQ 中 Connection 和 Channel 详解

    我们知道无论是生产者还是消费者,都需要和 RabbitMQ Broker 建立连接,这个连接就是一条 TCP 连接,也就是 Connection. 一旦 TCP 连接建立起来,客户端紧接着可以创建一个 ...

  8. print显示特定的数据格式

    原文网址 1.查看数据print  variable        查看变量print  *array@len      查看数组(array是数组指针,len是需要数据长度)可以通过添加参数来设置输 ...

  9. python学习(十九)常见的第三方库

    原文链接:http://www.limerence2017.com/2017/12/28/python19/#more 介绍几个python中常见的第三方库. Pillow Pillow简称PIL,是 ...

  10. Kubernetes--kubectl

    一.Kubectl命令行说明 类型 命令 描述 基础命令 create  通过文件名或标准输入创建资源 expose  将一个资源公开为一个新的kubernetes服务 run 创建并运行一个特定的镜 ...