首页 > 试题广场 >

懂二进制

[编程题]懂二进制
  • 热度指数:45893 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
示例1

输入

1999,2299

输出

7
classSolution {
public:
    /**
     * 获得两个整形二进制表达位数不同的数量
     *
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    string intToString32(intx){//整数转成32位的字符串
        intindex=31;
        string str="00000000000000000000000000000000";
        while(x>0){
            if((x&1)==1)
                str[index--]='1';
            else
                str[index--]='0';
        //str[index]=(x & 1);
            x=x>>1;
        }
        returnstr;
    }
     
    intcount(string str1,string str2){//比较两个32位字符串多少字符不同
        intc=0;
        for(inti=0;i<32;i++){
            if(str1[i]!=str2[i])
                c++;
        }
        returnc;
    }
     
    intcountBitDiff(intm, intn) {
        returncount(intToString32(m),intToString32(n));   
    }
};
发表于 2017-08-08 21:45:04 回复(0)
更多回答
1、异或
2、转为二进制字符串
3、替换字符串中的0
public class Solution {
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    public int countBitDiff(int m, int n) {
		String str=Integer.toBinaryString(m^n);
        str=str.replaceAll("0","");
		return str.length();
    }
}

发表于 2016-10-11 19:43:46 回复(10)
//JAVA 
public int countBitDiff(int m, int n) {

        int dif=m^n;//先将二者做异或运算,得到结果;
        int cnt=0;
        while(dif!=0){
            dif=dif&(dif-1);
            cnt++;
        }             //统计一个整数dif含有多少个1;
        return cnt;
    }

发表于 2015-04-25 15:45:02 回复(87)
int countBitDiff(int m, int n) 
{
	int len = 32;
	int difNum = 0;
	while(len)
	{
		int m_low = m&1; //取m最后一位,下同
		int n_low = n&1;
		if((m_low != n_low)) //直接两个数的最后一位对比
			difNum++;    //不同就计数+1
		m = m >> 1;   //m往右移动一位,下次循环就可以取倒数第二位,下同
		n = n >> 1;
		len--;
	}
	return difNum;
}

编辑于 2016-04-28 20:36:11 回复(14)
两个知识点:用位运算异或,两个数异或相同为0,不同为1.得到结果再去求1的个数
class Solution {
public:
    int countBitDiff(int m, int n)
    {
		int count=0;
        int val=m^n;
        while(val)
        {
            count++;
            val=val&(val-1);
        } 
        return count;
    }
};

编辑于 2016-04-05 19:39:37 回复(1)
public class test{
    public static int countBitDiff(int m,int n){
       int dif = m^n;
        int count = 0;
        for(int i=0;i<32;i++){
            if(((dif>>i)&1)!=0)
                count++;
        }
       return count;
   }
}
发表于 2015-05-09 19:09:37 回复(5)
int countBitDiff(int m, int n) {
		return numberOfOne(m^n);//m^n,m与n异或,其结果中不同位为1,相同位为0.
}
//统计整数a中数字1的个数
int numberOfOne(int a){
        int count = 0;
        while(a){
            a &= a-1;
            count++;
        }
        return count;
}

发表于 2016-03-27 21:06:12 回复(0)
    public int countBitDiff(int m, int n) {
		return Integer.bitCount(m^n);
    }
一行AC
发表于 2017-05-09 15:42:05 回复(1)
    public int countBitDiff(int m, int n) {
        int res = m ^ n;
        return Integer.bitCount(res);
    }

发表于 2018-06-15 21:01:29 回复(0)
class Solution {
public:
    // 存放int整数的二进制的32位
    vector<int> getBinary(int x)
    {
        vector<int> xBinary;
        for(int i=0; i<32; i++)
        {
            do{
                xBinary.push_back(x%2);
            }while(x/=2);
            if(x/2==0)
            {
                xBinary.push_back(0);
            }
        }
        return xBinary;
    }
    // 32位每位进行对比
    int countBitDiff(int m, int n)
    {
        int count=0;
        vector<int> mBinary, nBinary;
        mBinary = getBinary(m);
        nBinary = getBinary(n);
        for(int i=0; i<32; i++)
        {
            if(mBinary[i]!=nBinary[i])
            {
                count+=1;
            }
        }
        return count;
    }
};

发表于 2018-03-15 01:49:02 回复(0)
class Solution {
public:
    int countBitDiff(int m, int n) {
        int count = 0;
        while(m||n){//当有一者为0后,0%2=0,相当于给不够位补零了。
            if(m%2 != n%2)
                count++;
            m /= 2;
            n /= 2;
        }
        return count;
    }
};
/*排行第一的思路很精妙,异或运算将两个数中不同位标注为1,在通过按位与运算,统计1的个数,即不同位 个数。不过这种代码浓缩度高,不见得时间复杂度小,按位与运算计算机执行要比普通加减乘除复杂,需要每一位比较。不过思路还是值得学习。*/
class Solution {
public:
    int countBitDiff(int m, int n) {
        int mn,count = 0;
        mn = m^n;//异或,相同会变0,不同会变1,和数学上有区别,这里是按二进制每位进行异或处理。
        while( mn != 0 ){//不为零说明其中有1存在,而1的个数正是两个数二进制位不同的个数。
            mn &= (mn-1);//mn = mn&(mn-1),mn-1即将以最末尾的1变成了零。而&运算为按位与运算,两者相同位都为1,才为1,每次除去最末尾的1。
            count++;
        }
        return count;
    }
};

编辑于 2017-07-14 19:18:21 回复(3)
public class Solution {
/* 看是否同奇偶? */
    public int countBitDiff(int m, int n) {
        int count = 0;
        while(m != 0 || n != 0) {
            if(m%2!=n%2) { count++; }
            m/=2;
            n/=2;
        }
        return count;
    }
}

编辑于 2016-09-12 20:57:55 回复(1)
class Solution {
public:
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    int countBitDiff(int m, int n) {
        int diffs = 0;
        int xornum = m^n;
        while(xornum){
            if((xornum & 0x80000000) == 0x80000000){
                diffs ++;
            }
            xornum <<= 1;
        }
        
        return diffs;
    }
};

发表于 2016-08-03 20:17:12 回复(0)
int countBitDiff(int m, int n)
    {
        int iCount = 0;
        for(int i = 0; i != 32; ++i)
            if((m>>i&1) != (n>>i&1))
                ++iCount;         
        return iCount;
    }

编辑于 2015-07-12 12:36:41 回复(0)
直接统计m和n异或结果的二进制中有多少个1就可以了
public class Solution {
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    public int countBitDiff(int m, int n) {
        int x = m ^ n;
        int count = 0;
        while(x > 0){
            x = x&(x - 1);
            count ++;
        }
        return count;
    }
}


发表于 2021-02-27 14:03:37 回复(0)
public class Solution {
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    public int countBitDiff(int m, int n) {
        int tmp=m^n;
        if(tmp==0) return 0;
        int count=0;
        while(tmp!=0){
            count++;
            tmp=tmp&(tmp-1);
        }
        return count;
    }
}

发表于 2020-08-25 00:19:34 回复(0)
class Solution {
public:
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    int countBitDiff(int m, int n) {
        int sum=0;
        int c = n^m;
        while (c != 0) {
            if (c & 1 == 1) {
                sum++;
            }
            c = c >> 1;
        }
        return sum;
    }
};

发表于 2019-10-11 21:16:30 回复(0)
    public int countBitDiff(int m, int n) {
        int cnt = 0;
        // 按位异或,不同的位为1
        int x = m ^ n;
        while (x > 0) {
            // x&1使得只保留最低位,计数器加上该结果(即统计1个数)
            cnt += (x & 1);
            // 向右位移
            x >>= 1;
        }
        return cnt;
    }

发表于 2018-07-27 15:58:08 回复(0)
public int countBitDiff(int m, int n) {
        int x=m^n;
        String binStr=Integer.toBinaryString(x);
        char [] chars=binStr.toCharArray();
        int count=0;
        for(char c:chars){
            if(c=='1'){
                count++;
            }
        }
        return count;
    }

发表于 2018-06-26 19:21:29 回复(0)
int countBitDiff(int m, int n) {         int cnt=0;         while(m!=0 || n!=0){             if((m%2 != n%2)) cnt++;             m/=2;             n/=2;         }         return cnt;     }
};

发表于 2018-03-15 01:53:10 回复(0)
class Solution {
public:
    int countBitDiff(int m, int n) {         int result=m^n;
        int count=0;
        while(result)
        {
            count++;
            result=(result-1)&result;
        }
        return count;
    }
};

发表于 2018-01-15 15:18:30 回复(0)