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.

This is to test XOR(called: exclusive or)

Notice "=="'s order higher than "&"

 class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int res = 0;
int xor = a ^ b;
for (int i=0; i<32; i++) {
if (((xor>>i)&1) == 1)
res++;
}
return res;
}
};

Lintcode: Flip Bits的更多相关文章

  1. LintCode刷题笔记--Flip Bits

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

  2. 181. Flip Bits【easy】

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

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

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

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

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

  5. Flip Bits

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

  6. Lintcode: Update Bits

    Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits be ...

  7. [LintCode]——目录

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

  8. c++ bitset使用

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

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

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

随机推荐

  1. Amoeba基本配置

    Amoeba安装及读写分离配置一.amoeba简介官网:http://docs.hexnova.com/amoeba/index.html二.Centos下安装jdk1.yum 安装1.6版本jdk2 ...

  2. php 分词 —— PHPAnalysis无组件分词系统

    分词,顾名思义就是把词语分开,从哪里分开?当然是一大堆词语里了,一大堆词语是什么?是废话或者名言.这在数据库搜索时非常有用. 官方网站 http://www.phpbone.com/phpanalys ...

  3. C#Form窗体通过代码改变尺寸

    通过Size属性不能得到正确的窗体尺寸, 怎么办? 还需要设置 MaximumSize 属性和你的 size属性尺寸一样. this.FormBorderStyle = FormBorderStyle ...

  4. 蓝牙 BLE GATT 剖析(二)-- GATT UUID and 举例

    generic attribute profile (GATT)The Generic Attributes (GATT) define a hierarchical data structure t ...

  5. Delphi HOOK示例

    本应用程序的Hook: unit UFrmMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  6. oracle截取某个字符前面的字符串

    已验证. 要求:A.数据库表中的一个字符串 可能含有"+" 例:ORC+001 也可能不含“+” B.要求如果该字符串含有“+”,则取“+”之前的字符 例:ORC+001 取ORC ...

  7. EF扩展库(批量操作)

    EF删除和修改数据只能先从数据库取出,然后再进行删除 delete from Table1 where Id>5; update Table1 set Age=10; 我们需要这样操作 //删除 ...

  8. Intervals---poj1201(差分约束系统)

    题目链接:http://poj.org/problem?id=1201 题目说[ai, bi]区间内和点集Z至少有ci个共同元素,那也就是说如果我用Si表示区间[0,i]区间内至少有多少个元素的话,那 ...

  9. ORACLE十进制与十六进制的转换

    十进制与十六进制的转换 十进制-->十六进制 select to_char(100,'XX') from dual; 十六进制-->十进制select to_number('7D','XX ...

  10. 常用公共的css的样式

    html{-webkit-text-size-adjust:none; /*解决chrome浏览器下字体不能小于12px*/} body{overflow-x: hidden; font-size:1 ...