lintcode-easy-Flip Bits
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-easy-Flip Bits相关的知识,希望对你有一定的参考价值。
Determine the number of bits required to flip if you want to convert integer n to integer m.
Example
Given n = 31
(11111), m = 14
(01110), return 2
.
Note
Both n and m are 32-bit integers.
class Solution { /** *@param a, b: Two integer *return: An integer */ public static int bitSwapRequired(int a, int b) { // write your code here int count = 0; for(int i = 0; i < 32; i++){ int num1 = (a >> i) & 1; int num2 = (b >> i) & 1; if(num1 != num2) count++; } return count; } };
以上是关于lintcode-easy-Flip Bits的主要内容,如果未能解决你的问题,请参考以下文章