世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
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;
}
} class Solution: def countBitDiff(self , m: int, n: int) -> int: c = m ^ n #二进制同位数相同相同为0 不同为1 a = 0 while c: #求c中二进制有多少个1 c &= (c-1) #二进制同位数相同相同为1 不同为0 每次运算可以消除最低位数的那个1 直到结果=0 a += 1 return a
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @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') return Integer.toBinaryString(m^n).replaceAll("0","").length(); 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;
}
} 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
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);
}
};
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);
}
}; 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));
}
} 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
} 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;
}
}