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. 数据库MySql阶段总结

    S1数据库中最重要的是查询,对于查询要有一个好的理解模型是很关键的: 1. 每一个查询都会返回一个结果集,这个结果集可能是一个值,一个字段或者一个记录,甚至可能是一个表 返回一个值 SELECT * ...

  2. javaWeb中servlet开发——监听器

    监听的定义 对application的监听 application是servletContext接口的对象,表示的是整个上下文环境,如果要想实现对application监听则可以使用如下两个接口: s ...

  3. ②springMVC入门

    1 1.1      需求 以案例作为驱动. springmvc和mybaits使用一个案例(商品订单管理). 功能需求:商品列表查询 1.2      环境准备 数据库环境:mysql5.1

  4. SecureCRT登录Ubuntu 的中文乱码问题

    (1)/var/lib/locales/supported.d/local文件中添加一行:zh_CN.UTF-8 UTF-8,执行sudo locale-gen下载文件   su - root (2) ...

  5. Java程序设计的基本原则

    Java程序设计的基本原则-1 1.面向对象 这是java编程里面大家公认的第一原则 2.优先使用对象组合而非类继承 3.分层 最典型的三层架构,表现层-->逻辑层-->数据层 表现层功能 ...

  6. 【转】java正则表达式

    在Sun的Java JDK 1.40版本中,Java自带了支持正则表达式的包,本文就抛砖引玉地介绍了如何使用java.util.regex包. 可粗略估计一下,除了偶尔用Linux的外,其他Linu ...

  7. $watch、$digest、$apply

    $watch.$digest.$apply $watch 代表的就是对数据源的监听,当数据源发生变化,就会触发第二个参数的回调函数 $digest 代表触发一个数据源变化的事件 $apply 代表对于 ...

  8. C++ 安全字符串拼接

    #include <stdio.h> #include <stdint.h> #include <stdarg.h> #if defined(__GNUC__) # ...

  9. MANIFEST.MF详解(转)

    转载自http://blog.csdn.net/zhifeiyu2008/article/details/8829637 打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, ...

  10. How To Set Up Apache with a Free Signed SSL Certificate on a VPS

    Prerequisites Before we get started, here are the web tools you need for this tutorial: Google Chrom ...