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. 生命游戏/Game of Life的Java实现(转)

    首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...

  2. 高质量c/c++里的strcpy()

    已知strcpy函数的原型是:        char * strcpy(char * strDest,const char * strSrc);    1.不调用库函数,实现strcpy函数.    ...

  3. socket服务器开发中的SO_REUSEADDR选项与让人心烦的TIME_WAIT

    1 发现问题 我在开发一个socket服务器程序并反复调试的时候,发现了一个让人无比心烦的情况:每次kill掉该服务器进程并重新启动的时候,都会出现bind错误:error:98,Address al ...

  4. openstack 上床镜像, 创建网络, 创建虚拟机 命令

    ==================================================================================================== ...

  5. 【原创】大众点评监控平台cat的性能分析

    由于工作的原因,或者说我们之前内部监控设计和实现有点不满足现有的研发需求,所以调研了一下大众点评开源出来的cat这一套监控系统. 今天我们就来实验一把,cat的客户端埋点在我们的程序流程中上报数据到c ...

  6. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  7. SSL certificate problem unable to get local issuer certificate解决办法

    SSL certificate problem unable to get local issuer certificate 解决办法: 下载:ca-bundle.crt 将它放在自己的wamp或者x ...

  8. SVN同步大坑

    遇到的问题 这两天一直在搞svn的主从备份,使用的方法是svnsync做的主从同步,同步大部分的仓库都没有什么问题很顺利的就同步完成了,不了解svnsync同步的可以看我这篇,但是在在同步2个仓库的时 ...

  9. Mac OS X 中vim开启配色和git补全

    问题起源:mac 下自带的git 无法补全命令,配色方案不完善 Mac OS Yosemite 10.10.3 git version 2.3.2 (Apple Git-55) 处理结果:git命令可 ...

  10. WEB安全--CSRF剖析

    CSRF攻击:攻击者构造合法的HTTP请求,随后利用用户的身份操作用户帐户的一种攻击方式. 一.CSRF攻击原理CSRF的攻击建立在浏览器与Web服务器的会话中:欺骗用户访问URL.二.CSRF攻击场 ...