码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode-easy-Flip Bits

时间:2016-02-24 19:29:44      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/goblinengineer/p/5213930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!