首页 > 试题广场 >

懂二进制

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

输入

1999,2299

输出

7
public class Solution {
    public int countBitDiff(int m, int n) {
        int res=0;
        while(m!=0||n!=0){
            if((m&1)!=(n&1)){
                res++;
                m>>>=1;
                n>>>=1;
            }else{
                m>>>=1;
                n>>>=1;
            }
        }
        return res;
    }
}

发表于 2020-08-30 11:28:44 回复(0)
 //先将m和n进行与得到k
// 然后求k里的个数,从右往左判断。

 public int countBitDiff(int m, int n) {
        int count=0;
        int k= m^n;
        while(k!=0){
           int temp=k&1;
           if(temp==1)    count++;
            k>>>=1;

        }
        return  count;
    }
}


发表于 2020-08-26 17:00:40 回复(0)
 public int countBitDiff(int m, int n) {
        int a = m ^ n;
        int result = 0;
        for (int i =0; i < 32 ; i++){
            result = result +((a>>i)&1);  // 逐步移位移到最低位再与1相与
        }
        return result; 
    }

发表于 2019-03-29 11:49:00 回复(0)
1.先转为长度为32的字符串
2.比较两个字符串每一位
代码如下:
public static int countBitDiff(int m, int n) {
        String strn = "";
        String strm = "";
        int i=0;
        while(i++ < 32){
            strn+=n&1;
            strm+=m&1;
            n=n>>1;
            m=m>>1;
        }
        // strn, strm 都是 n,m 二进制表示形式到反转形式,不影响比较结果
        int count=0;
        for(i=0;i<32;i++){
            if(strn.charAt(i)!=strm.charAt(i))
                count ++;
        }
        return count;
    }

发表于 2018-08-19 17:30:38 回复(0)
    先进行异或操作,得到所有不同位;然后每次取出末尾,统计1的位数。
    public int countBitDiff(int m, int n) {
        int result = 0;
        int value = m ^ n;
        while (value != 0) {
            result += value & 1;
            value = value >> 1;
        }
        return result;
    }

发表于 2018-04-15 15:39:39 回复(0)
//对两个数做异或,然后统计各位上1的个数
public class Solution {
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    public int countBitDiff(int m, int n) {
        int num = m ^ n;
        int res = 0;
        while(num != 0){
            if(num % 2 == 1){
                res++;
            }
            num = num / 2;
        }
        return res;
    }
}

发表于 2017-09-13 21:53:28 回复(0)
    public int countBitDiff(int m, int n) {
        // 方法一:两数异或,从低位到高位移位1判断1的个数
        // 跟用自身判断的方法相比还是不够简洁且麻烦,大神何其多啊!
        int mXORn = m ^ n;
        int count = 0;
        for (int i = 1; i != 0; i <<= 1) {
            if ((i & mXORn) != 0) {
                count++;
            }
        }
        return count;
        // 方法二:直接调用Integer.bitCount()方法
        // 太惊喜了,居然API里Integer类非构造方法的第一个方法就能搞定!如下:
        // static int bitCount(int i)
        // 返回指定int值的二进制补码表示形式的 1 位的数量。
        // return Integer.bitCount(m ^ n);
    }

编辑于 2017-08-01 11:52:46 回复(0)
写的虽然繁琐,但是思路清晰。我很笨,只能这样一步步来了。
public static int countBitDiff(int m, int n) {
        
        int count=0;
		
		String s1 = Integer.toBinaryString(m);
		String s2 = Integer.toBinaryString(n);
		char[] strChar1 = s1.toCharArray();
		char[] strChar2 = s2.toCharArray();
		if(strChar1.length<strChar2.length){
			
			for(int i=0;i<strChar2.length-strChar1.length;i++){
				if(strChar2[i]!='0'){
					count++;
				}
			}
			int j=0;
			while(j<strChar1.length){
				if(strChar1[strChar1.length-1-j]!=strChar2[strChar2.length-1-j]){
					count++;
				}
				j++;
			}
			return count;
		}else if(strChar1.length>strChar2.length){
			for(int i=0;i<strChar1.length-strChar2.length;i++){
				if(strChar1[i]!='0')
					count++;
			}
			int j=0;
			while(j<strChar2.length){
				if(strChar2[strChar2.length-1-j]!=strChar1[strChar1.length-1-j]){
					count++;
				}
				j++;
			}
			return count;
		}else{
			for(int j=strChar2.length-1;j>=0;j--){
				if(strChar1[j]!=strChar2[j])
					count++;
			}
			return count;
		}
    }

发表于 2017-07-18 15:57:34 回复(4)
public class Solution {
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    public int countBitDiff(int m, int n) {
		int result = n^m;
		int num = 0;
		while(result != 0){
			if((result&1) == 1)num ++;
			result = result>>1;
		}
        return num;

    }
}

发表于 2017-07-12 19:48:44 回复(0)
   //求各位解答问题,为什么编译不通过呢??
public int countBitDiff(int m, int n) {
        int diff=m^n;
        int count=0;
        int diff_lastbit=0;
        //boolean bl=false;
        
        while(diff!=0)//如何判断diff长度为零了呢
            diff_lastbit=diff&1;
          {  if(diff_lastbit==1)  
              count++;
              diff=diff>>1;
          }
         return  count;
    }
}

发表于 2017-03-31 18:12:20 回复(0)

public class Solution { /**

 * 获得两个整形二进制表达位数不同的数量
 * 
 * @param m 整数m
 * @param n 整数n
 * @return 整型
 */
public int countBitDiff(int m, int n) {
    int k=0;
    int count=0;
    k=m^n;
    while(k!=0){
        k=k&(k-1);
        count++;
    }
    return count;
}

}

编辑于 2017-03-30 19:57:59 回复(0)
import java.util.Scanner;

public class Solution {
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    public int countBitDiff(int m, int n) {
       int re=m^n;
return re;
}
int findNumber(int m){
int num = 0;
int flag;
while(m != 0){
flag = m % 2;
m = m / 2;
if(flag == 1){
num ++;
}
}
return num;
}
   public static void main(String[] args){
{
   Scanner in=new Scanner(System.in);  
  Solution so=new Solution();
   int n=in.nextInt();
   int m=in.nextInt();
   System.out.print(so.findNumber(so.countBitDiff(m, n)));
}
}
发表于 2017-03-29 10:25:21 回复(0)

/**

  • 题目描述:世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么? 解题思路:判断两个数的位数相同一般使用异或操作,对异或得到的数移位和1分别进行“与”操作 */ public class Day2017032801 { public static int countBitDiff(int m, int n) {

      m = m^n;
     int num = 0;
     while (m>0){
         if( (m&1) == 1) num++;
         m = m>>1;
     }
     return num;
    

    }

    public static void main(String [] args){

     int m=5;
     int n=2;
     System.out.println(countBitDiff(m,n));
    

    } }

发表于 2017-03-28 13:39:28 回复(0)
public class Solution {
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    public int countBitDiff(int m, int n) {
		return Integer.toBinaryString(m^n).replace("0", "").length();
    }
}

发表于 2017-03-26 11:32:19 回复(0)
/**
    * 获得两个整形二进制表达位数不同的数量
    *
    * @param m 整数m
    * @param n 整数n
    * @return 整型
    */
    public int countBitDiff(int m, int n) {
        int counter = 0;
        int diffOr = m ^ n;
        while (diffOr != 0) {
             if ((diffOr & 1) == 1) {//判断末尾是否为1
                counter++;
            }
            diffOr >>>= 1;
        }
        return counter;
}
编辑于 2017-03-19 21:31:24 回复(0)
public class Solution {
    /**
     * 获得两个整形二进制表达位数不同的数量
     * 
     * @param m 整数m
     * @param n 整数n
     * @return 整型
     */
    public int countBitDiff(int m, int n) {
        int count = 0;
        for (int i=0;i<32;i++){
            if (m%2!=n%2){
                count++;
            }
            m = m>>>1;
            n = n>>>1;
        }
        return count;
    }
}
发表于 2017-03-08 17:45:44 回复(0)
public int countBitDiff(int m, int n) {
        final int different = m ^ n;
        int count = 0;
        for(int i = 0; i < 32; i++) {
            if((different & (1 << i)) != 0) {
                count++;
            }
        }
        return count;
 }
发表于 2017-03-08 16:00:55 回复(0)
    public int countBitDiff(int m, int n) {
int res = m ^ n;
        int count = 0;
        for(int i = 0; i < 32 && res!= 0; i++) {
            count += res % 2;
            res /= 2;
        }
        
        return count;
    }
发表于 2017-02-19 16:47:52 回复(0)