5.5 Write a function to determine the number of bits required to convert integer A to integer B.
EXAMPLE
Input: 31,14
Output: 2

这道题给了我们两个数字A和B,问如果将A转化为B需要变几个位,那么我们很容易想到要用异或来做,因为相同位异或为0,那么为1的为就是不相同的位,总和就是我们要求的结果。那么此题就转化为求异或结果中位‘1’的个数,我们可以用for循环来做,判断异或数的最低位是否为1,若为1就计数器加1,然后将异或数向右移动一位,以此类推直至异或数为0,参见代码如下:

解法一:

class Solution {
public:
int bitConvertNumber(int a, int b) {
int res = ;
for (int c = a ^ b; c != ; c >>= ) {
res += c & ;
}
return res;
}
};

还有一种方法,这里不是将异或数每次向右移动一位,而是用n&(n-1)来清楚最低有效位Least Significant Bit,每清除一个,计数器增1,直到异或数为0停止,参见代码如下:

解法二:

class Solution {
public:
int bitConvertNumber(int a, int b) {
int res = ;
for (int c = a ^ b; c != ; c &= c - ) {
++res;
}
return res;
}
};

Tips:根据之前那篇Single Number III 单独的数字之三,我们知道得到最低有效位Least Significant Bit的方法是n&(-n),而这道题又学习了清除最低有效位Least Significant Bit的方法是n&(n-1),这些小技巧需要我们深刻理解,并在今后的解题中灵活的使用,这才是刷题的精髓所在。。。

[CareerCup] 5.5 Number of Converted Bits 转换数字所需的位数的更多相关文章

  1. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  2. LeetCode 191:number of one bits

    题目就是: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

  3. 【Leetcode_easy】693. Binary Number with Alternating Bits

    problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...

  4. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  5. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  6. Number of 1 Bits(Difficulty: Easy)

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  7. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  8. LeetCode Number of 1 Bits

    原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...

  9. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

随机推荐

  1. TXT记事本转换PDF文件

    使用的方式为,读取TXT记事本的内容,然后写入创建的PDF文件. static void Main(string[] args) { const string txtFile = "D:\\ ...

  2. HttpClient使用方法(包括POST文件)

    最近在做跨系统的数据交互业务,从.Net的系统提交数据到Java的系统. 简单的表单Get.POST都没问题,但是有个功能是要提交普通文本和文件,试了好多都有问题,最后用HttpClient小折腾了一 ...

  3. mysql,sql server,oracle 唯一索引字段是否允许出现多个 null 值?

    最近一个项目,涉及到sql server 2008,因为业务需求,希望建立一个唯一索引,但是发现在sql server中,唯一索引字段不能出现多个null值,下面是报错信息: CREATE UNIQU ...

  4. Spring配置文件外部化配置及.properties的通用方法

    摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...

  5. Linux命令的类型

    1.内建命令: 由shell程序自带的命令,最常见的有cd.pwd等. 使用type命令即可查看命令属于哪种,比如: #type cd cd is a shell builtin ————>看到 ...

  6. 动手学习TCP:服务端状态变迁

    上一篇文章介绍了TCP状态机,并且通过实验了解了TCP客户端正常的状态变迁过程. 那么,本篇文章就一起看看TCP服务端的正常状态变迁过程 服务端状态变迁 根据上一篇文章中的TCP状态变迁图,可以得到服 ...

  7. 译文 对无障碍网页应用(ARIA)的选择

    //本文编辑格式为Markdown,译文同时发布在众成翻译 对无障碍网页应用(ARIA)的选择 让网站对每个人都能访问是一件相当艰难的工作,尤其是在我们使用自定义标记解决方案(custom marku ...

  8. IOS Orientation, 想怎么转就怎么转~~~

    此博文主要针对IOS应用, 是屏幕旋转相关问题的一个总结. 主要内容有: IOS5,6,7不同版的适配. 强制旋转和自动旋转. 博客: http://www.cnblogs.com/jhzhu 邮箱: ...

  9. 2014 Super Training #4 G What day is that day? --两种方法

    原题: ZOJ 3785 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3785 题意:当天是星期六,问经过1^1+2^2+ ...

  10. FlashInspector 【Firefox浏览器插件,flash分析工具】

    Inspect flash(swf)'s DisplayObject with mouse. Overview the swf's DisplayObject list. Set the inspec ...