首页 > 试题广场 >

懂二进制

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

输入

3,5

输出

2

说明

3的二进制为11,5的二进制为101,总共有2位不同 
示例2

输入

1999,2299

输出

7
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param m int整型 
     * @param n int整型 
     * @return int整型
     */
    public int countBitDiff (int m, int n) {
        // write code here
        int c = 0;//不同位的个数
        int t = m ^ n;//异或:相同为0,相异为1,所以异或完之后,有几个1就有几个不同位
        //求1的个数的妙招
        //t = t & (t - 1)
        while(t != 0) {
            t = t & (t - 1);
            c++;
        }
        return c;
    }
}


发表于 2022-06-25 20:46:18 回复(0)
这个题是有点问题的,当长度不一样时候,短的二进制前面补充0是不对的
发表于 2023-11-30 20:13:53 回复(0)
# 通过异或运算得出不同位数,在进行统计
class Solution:
    def countBitDiff(self , m: int, n: int) -> int:
        # write code here
        c = bin(m^n).count('1')
        return c

发表于 2022-03-29 10:51:32 回复(0)
世界上有10种人,一种懂二进制,一种不懂,剩下8种是懂一点点,懂的不多?还是和功力一样,一层懂,二层懂,到九层懂,完全懂就是一重天了?
编辑于 2024-04-25 11:16:11 回复(0)
class Solution {
public:
     int countBitDiff(int m, int n) {
    int temp=m^n;
    int ans=0;
    while (temp) {
    temp=temp&temp-1;
    ans++;
    }
    return ans;
    }
};

编辑于 2024-04-11 15:53:56 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param m int整型 
# @param n int整型 
# @return int整型
#
class Solution:
    def countBitDiff(self , m: int, n: int) -> int:
        # write code here
        tmp = m ^n
        # print(bin(tmp))

        return bin(tmp)[2:].count('1')

发表于 2024-04-03 23:28:31 回复(0)
using System;
using System.Collections.Generic;

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param m int整型
     * @param n int整型
     * @return int整型
     */
    public int countBitDiff (int m, int n) {
        // write code here
        string strMB = Convert.ToString(m, 2).PadLeft(32, '0');
        string strNB = Convert.ToString(n, 2).PadLeft(32, '0');
        int nMinLen = strMB.Length > strNB.Length ? strNB.Length : strMB.Length;
        int nMaxLen = strMB.Length > strNB.Length ? strMB.Length : strNB.Length;
        int nCount = 0;
        for (int i = 0; i < nMinLen; i++) {
            if (strMB[i] == strNB[i])
                continue;
            nCount++;
        }
        return nCount + nMaxLen - nMinLen;
    }
}
发表于 2024-03-29 22:15:55 回复(0)
为什么这里正确答案是22而不是23位?有没有懂的兄弟可以解释一下的
编辑于 2024-03-21 17:14:45 回复(0)
return Integer.toBinaryString(m^n).replaceAll("0","").length();

发表于 2024-03-17 15:04:49 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param m int整型 
     * @param n int整型 
     * @return int整型
     */
    public int countBitDiff (int m, int n) {
        // write code here
        int res = 0;
        while((m > 0) || (n > 0)) {
            if((m & 1) != (n & 1) ) res++;
            m = m >> 1;
            n = n >> 1;
        }
        return res;
    }
}

发表于 2024-02-04 02:44:13 回复(0)
    let r = (n ^ m).toString(2);
    let reg = new RegExp("1", "g");
    let c = r.match(reg);
    return (c || []).length;
编辑于 2024-01-31 10:19:57 回复(0)
class Solution:
    def countBitDiff(self, m: int, n: int) -> int:
        # write code here
        count = 0
        rM = bin(m & 0xFFFFFFFF)[2:].zfill(32)
        rN = bin(n & 0xFFFFFFFF)[2:].zfill(32)
        for val1, val2 in zip(rM, rN):
            if val1 != val2:
                count += 1
        return count

编辑于 2024-01-15 16:00:14 回复(0)

class Solution {

public:

/**

 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可

 *

 *
  • @param m int整型

  • @param n int整型

  • @return int整型

    */

    int count_bit_one(int n)

    {

      int count = 0;
    
      while (n)
    
      {
    
          n = n & (n - 1);
    
          count++;
    
      }
    
      return count;

    }

    int countBitDiff(int m, int n) {

      // write code here
    
      int x = m^n;
    
      return count_bit_one(x);

    }

};

发表于 2023-07-20 17:29:50 回复(0)
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param m int整型 
     * @param n int整型 
     * @return int整型
     */
    int countBitDiff(int m, int n) {
        uint tmp = m ^ n;
        tmp = (tmp &010101010101)
        +((tmp>>1)&010101010101)
        +((tmp>>2)&010101010101)
        +((tmp>>3)&010101010101)
        +((tmp>>4)&010101010101)
        +((tmp>>5)&010101010101);

        return (tmp%63);
    }
};

发表于 2023-07-03 20:51:27 回复(0)
import java.util.*;
import java.lang.Math;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param m int整型
     * @param n int整型
     * @return int整型
     */
    public int countBitDiff (int m, int n) {
        long num = 0;
        long x = 0;
        String twoM = String.format("%32s", Integer.toBinaryString(m)).replace(' ',
                      '0');
        String twoN = String.format("%32s", Integer.toBinaryString(n)).replace(' ',
                      '0');

        long lengthM = twoM.length();
        long lengthN = twoN.length();
        long lengthMin = Math.min(lengthM, lengthN);
        for (int i = 0; i < lengthMin; i++) {
            if (twoM.charAt(i) != twoN.charAt(i)) {
                num++;
            }
        }
        return (int)(num + Math.abs(lengthM - lengthN));
    }
}

发表于 2023-03-30 20:47:18 回复(0)
package main
import "strconv"

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param m int整型 
 * @param n int整型 
 * @return int整型
*/
func countBitDiff( m int ,  n int ) int {
    s:=strconv.FormatInt(int64(m^n),2)
    ans:=0
    for _,ch:=range []byte(s){
        if ch=='1'{
            ans++
        }
    }
    return ans
}

发表于 2023-03-09 22:44:10 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param m int整型 
     * @param n int整型 
     * @return int整型
     */
    public int countBitDiff (int m, int n) {
        // write code here

        // 1. 异或操作,获取 m和n 都有哪些位是不同的
        int temp = m ^ n;
        int res = 0;
        // 2. 获取temp上有多少个1,有多少个1,就有多少个不同
        while(temp>0){
            // 将temp二进制,先右移1位,再左移1位,
            int tt = temp;
            temp = temp>>1;
            // 如果temp右移前末尾为1,两次移动结果必然不一致
            if(tt != temp<<1){
                res++;
            }
        }


        return res;
    }
}
发表于 2023-02-28 19:20:16 回复(0)
//负数在内存中存储的就是补码
//先是计算两个数的异或结果 相同为0 不同为1。最后根据这结果判断有几个1.
int countBitDiff(int m, int n ) {
// write code here
int i=0;
//int ans=0;
int ans=m^n;
int count=0;
while(i<32){
count+=(ans&1);
i++;
ans=ans>>1;

}
return count;
}
发表于 2022-12-23 17:08:25 回复(0)
class Solution:
    def countBitDiff(self , m: int, n: int) -> int:
        # write code here
        ret = 0
        xor = m ^ n
        while xor != 0:
            if xor & 1:
                ret += 1
            xor >>= 1
        return ret

发表于 2022-09-29 02:06:19 回复(0)
int countBitDiff(int m, int n ) {
    // write code here
    int count=0;
    int s=m^n;
    int mask;
    for(int i=0;i<32;i++)
    {
        mask=1<<i;
        if(s&mask) count++;
    }
    return count;
}
发表于 2022-08-08 09:53:36 回复(0)

问题信息

上传者:牛客301499号
难度:
26条回答 1544浏览

热门推荐

通过挑战的用户

查看代码