Lintcode: Flip Bits
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的更多相关文章
- LintCode刷题笔记--Flip Bits
Flip Bits: 标签:位运算 题目:Determine the number of bits required to flip if you want to convert integer n ...
- 181. Flip Bits【easy】
181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n ...
- 181. Flip Bits【LintCode, by java】
Description Determine the number of bits required to flip if you want to convert integer n to intege ...
- lintcode:Flip Bits 将整数A转换为B
题目: 将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 样例 如把31转换为14,需要改变2个bit位. ()10=()2 ()10=()2 挑战 你能想出几种方法? 解题: A- ...
- Flip Bits
Determine the number of bits required to flip if you want to convert integer n to integer m. Notice ...
- 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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- c++ bitset使用
A bitset is a special container class that is designed to store bits (elements with only two possibl ...
- 黑科技--位集--bitset
自从上次网赛发现这么个东西之后,深深地感受到了bitset的强大,0.0. 正常的bool占用1字节空间,bitset可以把这个缩到1bit,空间上8倍优化.正常用起来可能会跟位运算状态压缩类似,但是 ...
随机推荐
- 基于LR的Oracle应用性能测试
最近对一个oracle ERP系统的INV模块进行性能测试,因为之前大部分都是测试web类型的应用,在这方面经验较少,期间也遇到了不少问题,因此有必要作些总结,以备后忘.首先先简单了解下测试对象相关的 ...
- register instruction pointer
Computer Science An Overview _J. Glenn Brookshear _11th Edition We have already encountered the conc ...
- Android之Fragment学习笔记①
Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...
- 蓝牙—服务发现协议(SDP)
服务搜索协议(SDP)提供了应用发现可用服务以及确定可用服务特点的方法.SDP发现协议提供下面的能力 <1>为客户提供搜索所需要服务的能力. <2>允许基于服务类型搜索服务 & ...
- 蓝牙的HFP协议笔记
1.概述 HFP(Hands-free Profile),可以让蓝牙设备可以控制电话,如接听.挂断.拒接.语音拨号等,拒接.语音拨号要视蓝牙耳机及电话是否支持. HFP定义了音频网关(AG)和 ...
- Docker Device Mapper 使用 direct-lvm
一.Device Mapper: loop-lvm 默认 CentOS7 下 Docker 使用的 Device Mapper 设备默认使用 loopback 设备,后端为自动生成的稀疏文件,如下 ...
- 【Android开发学习笔记】【高级】【随笔】插件化——资源加载
前言 上一节我们针对插件最基本的原理进行了一个简单的demo实现,但是由于插件的Context对象被宿主所接管,因此无法加载插件程序的资源.那么如何解决这个问题捏? 有人提出这样的方案:将apk中的资 ...
- CSS3新添加的属性
1.圆角设置 border-radius:15px 50px 30px 5px; /*四个值: 第一个值为左上角,第二个值为右上角,第三个值为右下角,第四个值为左下 角. 三个值: 第一个值为左上角, ...
- android Textview动态设置大小
import android.app.Activity; //import com.travelzen.tdx.BaseActivity; //import com.travelzen.tdx.uti ...
- Linux-modules software
简介 这里指的modules不是linux内核相关的module,只是用于软件多版本控制的一个开源软件包,比如说系统同时有neo4j的不同版本,使用modules软件就可以使得在需要的时候选择相应的软 ...