LintCode: Flip Bits
C++
1 class Solution { 2 public: 3 /** 4 *@param a, b: Two integer 5 *return: An integer 6 */ 7 int bitSwapRequired(int a, int b) { 8 // write your code here 9 unsigned int tmpA = a; 10 unsigned int tmpB = b; 11 int steps = 0; 12 while (tmpA || tmpB) { 13 steps += (tmpA&0x1)^(tmpB&0x1); 14 tmpA >>= 1; 15 tmpB >>= 1; 16 } 17 return steps; 18 } 19 };
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。