181- 整数A转化为整数B

来源:互联网 发布:怎么开淘宝直播间 编辑:程序博客网 时间:2024/05/16 17:11

4.6

刚开始忽略了正数负数的问题,循环结束的条较为 tmp > 0 ,是不对的。

后来改成了循环32次。

class Solution {    /**     *@param a, b: Two integer     *return: An integer     */    public static int bitSwapRequired(int a, int b) {        int count = 0;        int tmp = a^b;        for(int i = 0 ;i<32;i++){            if((tmp & 1) == 1 ){                count ++;            }            tmp >>=1;        }        return count;// write your code here    }};



0 0