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奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5006644.html,如需转载请自行联系原作者

相关文章
|
存储 C语言
【CSAPP随笔】CH3:Bits, Bytes, and Integers
【CSAPP随笔】CH3:Bits, Bytes, and Integers
74 0
LeetCode 190. Reverse Bits
颠倒给定的 32 位无符号整数的二进制位。
88 0
LeetCode 190. Reverse Bits
[LeetCode]--191. Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11’ has binary representation
1106 0