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. Delphi下使用MapWinGIS控件打开GIS图层

    unit Unit3; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  2. C++ char*,const char*,string的相互转换

    1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string   ...

  3. 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题

    在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...

  4. 1014 C语言文法

    <程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> ...

  5. 转一个 C#基础类库

    转自:http://www.cnblogs.com/sufei/archive/2012/12/07/2807170.html http://www.sufeinet.com/thread-655-1 ...

  6. HttpContext为null new HttpContextWrapper(System.Web.HttpContext.Current)

    HttpContext = (context == null ? new HttpContextWrapper(System.Web.HttpContext.Current) : context);

  7. zepto源码--isEmptyObject,isNumeric,inArray,trim--学习笔记

    1.isEmptyObject,判断对象是否为空对象的函数 定义变量name,遍历传入对象的属性name,如果存在任何属性,则返回false,判定传入的参数为非空对象,否则即为空对象. 2.isNum ...

  8. Linux 下动态库 / 静态库(依赖)

    一. 依赖动态库的动态库 libfun.so依赖动态库libtest.so(libfun.so动态库里的函数intnothing()调用了libtest.so里的intmytest()函数),而mai ...

  9. QFile文件操作-QT

    #include <QCoreApplication> #include<QFile> #include<QString> #include<QDebug&g ...

  10. Github 与Git pages

    基础git命令 设置username,email $ git config --global user.name "your name" $ git config --global ...