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. js事件执行顺序

    http://ejohn.org/blog/how-javascript-timers-work/

  2. HashMap总结

    最近朋友推荐的一个很好的工作,又是面了2轮没通过,已经是好几次朋友内推没过了,觉得挺对不住朋友的.面试反馈有一方面是有些方面理解思考的还不够,平时也是项目进度比较紧,有些方面赶进度时没有理解清楚的后面 ...

  3. 【mysql】关于IO/内存方面的一些优化

    这里使用的是mysql  Ver 14.14 Distrib 5.6.19, for Linux (i686) using  EditLine wrapper 一.mysql目录文件 ibdata1: ...

  4. fis自动化部署

    1,自动部署到远程服务器 (1),参考:https://github.com/fex-team/receiver (2),接收服务代码目录:/var/www/html/fis/receiver-mas ...

  5. ASP.NET调用Web Service

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  6. TCP连接与关闭

    1.建立连接协议(三次握手) (1)客户端发送一个带SYN标志的TCP报文到服务器.这是三次握手过程中的报文1. (2) 服务器端回应客户端的,这是三次握手中的第2个报文,这个报文同时带ACK标志和S ...

  7. Mysql在windows下和linux下对表名大小写默认要求的一个细节

    今天在虚拟机里搭建项目环境,偷了下懒,直接把本机数据库中的表用sqlyog复制给虚拟机中的数据库,然后开始部署项目,项目一启动提示: Table 'sdmqrt.QRTZ_LOCKS' doesn't ...

  8. [转]后台页面访问权限:页面基类&内置票据认证 使用方法

    本文转自:http://www.cnblogs.com/fishtreeyu/archive/2011/01/29/1947421.html 一般网站后台页面除了登录页面login.aspx未登录用户 ...

  9. 【NOIP合并果子】uva 10954 add all【贪心】——yhx

    Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...

  10. 《TCP/IP详解 卷一》读书笔记-----TCP数据流

    1.Delayed Acknowledgements:TCP通常不会在收到数据之后立即返回一个ACK,而是会有一个延时,希望能ACK报文段中带上一些数据,通常这个延时为200ms 2.Nagle Al ...