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. Defining Stored Programs

    ok DROP PROCEDURE IF EXISTS truncate_insert_rank_month; DELIMITER /w/ CREATE PROCEDURE truncate_inse ...

  2. MySql解决插入中文乱码问题

    在dos中登陆mysql 后输入: // 查看数据使用的所有编码show variables like 'character%';// 修改客户端的编码 为 gbkset character_set_ ...

  3. nginx https

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中.通常这 ...

  4. JS性能消耗在哪里?

    内部原因:构造,递归,循环,拷贝,动态执行,字符串操作等   1.过度的封装(过多的创建“庞大的”对象,但是如果在允许的条件下,面向对象的封装是可以提高维护性,而且符合我们的高内聚低耦合原则): 2. ...

  5. centos 安装 openerp

    遇到问题:近日公司提出openerp的搭建,觉得openerp里的有些模块比较适合公司,openerp的运作,估计会有利于公司系统化的管理.于是我就去了解openrp,然后来搭建这套强大的系统. 解决 ...

  6. C++中string转化为常用数值类型

    //模板类 用于将string类型转化为 常用数值类型 template <class Type> Type stringToNum(const string& str) { is ...

  7. The Best Path---hdu5883(欧拉路径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5883 题意:n 个点 m 条无向边的图,找一个欧拉通路/回路使得这个路径所有结点的异或值最大. 先判断 ...

  8. 如何设置启动页 LaunchImage

  9. centos 6.4 安装视频解码器

    cd /etc/yum.repos.d/ wget http://mirrors.163.com/.help/CentOS6-Base-163.repo yum update rpm -Uhv htt ...

  10. Power-BI费用分析

    费用分析主要从财务三大费用入手,剖析费用的结构.用途.占用等情况,从三大费用到明细费用.部门.职员的层层钻取,从而有效地进行费用管理和控制.Power-BI前端展示:图1<ignore_js_o ...