[编程题]bit位数

##### 输入描述:
`一行中给定两个数字`

##### 输出描述:
`输出这两个数字中bit不同的个数`

`15 8`

`3`

## python3两行

``````a, b = map(lambda c: bin(int(c)).replace("0b", "").rjust(32, '0'), input().split())
print(sum(map(lambda c: a[c] != b[c], range(32))))``````
• 第一行：统一格式，将a，b转化成32位二进制格式字符串

• 第二行，对32位分别进行计算判断是相同，将结果累加。

```import java.util.*;

public class Main {
private static final int MAX = 1005;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt(), n2 = sc.nextInt();
int count = 0;
for (; n1 != 0 || n2 !=0; n1 >>= 1, n2 >>= 1) {
if ((n1 & 1) != (n2 & 1)) {
count++;
}
}
System.out.println(count);
}
}
```

```import java.util.Scanner;

/**
* @author wylu
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int xor = scanner.nextInt() ^ scanner.nextInt();
int count = 0;
while (xor != 0) {
xor &= (xor - 1);
count++;
}
System.out.println(count);
}
}
}

```

import java.util.Scanner;

public class Main {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int sum=0;
String ml = Integer.toBinaryString(m);
String nl = Integer.toBinaryString(n);
int q = new StringBuffer(ml).length() > new StringBuffer(nl).length() ? new StringBuffer(ml).length():new StringBuffer(nl).length();
char[] mArr = new char[q];
char[] nArr = new char[q];
for(int i=0;i<q;i++){
mArr[i]=nArr[i]='0';
}

for(int i=q-1,j=ml.length()-1;i>=0 && j>=0;i--,j--){
mArr[i] = ml.charAt(j);
}

for(int i=q-1,j=nl.length()-1;i>=0 && j>=0 ;i--,j--){
nArr[i] =nl.charAt(j);
}

//        for(int i =0;i<q;i++){
//            System.out.print(mArr[i]+" ");
//        }
//        System.out.println();
//        for(int i =0;i<q;i++){
//            System.out.print(nArr[i]+" ");
//        }
//
for(int i=0;i<q;i++){
if(mArr[i]!=nArr[i]){
sum++;
}
}
System.out.println(sum);
}
}

```#include <bits/stdc++.h>

using namespace std;

int main()
{     int a,b;     while(cin>>a>>b)      {         int cnt = 0;         int c = a^b;         while(c)         {             cnt += (c&1);             c>>=1;         }         cout<<cnt<<endl;     }     return 0;
}```

m,n=map(int,input().split())
b=[int(x) for x in '{0:b}'.format(m^n)]
print(b.count(1))

```package main

import "fmt"
import "os"
import "bufio"
import "strconv"
import "strings"

func getResult(m int, n int) int {
var count int
for {
if(m == 0 && n == 0) {
break
}
if((m % 2) != (n % 2)) {
count++
}
m = m >> 1
n = n >> 1
}
return count
}

func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
s := scanner.Text()
str_slice := strings.Fields(s)
m,_ := strconv.Atoi(str_slice[0])
n,_ := strconv.Atoi(str_slice[1])
res := getResult(m, n)
fmt.Printf("%d\n",res)
}
}
```

#include<bits/stdc++.h>
int main(void)
{
int32_t m, n;
scanf("%d%d", &m, &n);
int32_t num = m ^ n;
int time = 0;
while(num) {
time += num & 1;
num >>= 1;
}
printf("%d\n", time);
return 0;
}

```#include<stdio.h>
int main()
{
int m = 0, n = 0;
int or = 0, sum = 0;

scanf("%d%d",&m,&n);
or = m ^ n;

while(or)
{
sum += or & 0x01;
or >>= 1;
}
printf("%d\n",sum);
return 0;
}

```

```#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
int c=a^b;
int count=0;
for(int i=0;i<32;i++)
{
count+=c&1;
c=c>>1;
}
cout<<count<<endl;
}
```

```readline = list(map(int, input().split()))
diff = 0

for i in range(32):
if a[i] != b[i]:
diff += 1
print(diff)
```

```import java.util.*;

public class Main{
/**
将整数解析为二进制数组，总长32位即4字节
*/
public static int[] parseBinary(int num) {
int[] b = new int[32];
int index = 31 ;

while(num > 0) {
b[index] = num % 2 ;
num /= 2 ;
index-- ;
}
/*
for(int i = 0 ; i < 32 ; i++ ) {
System.out.print(b[i]);
}System.out.println();*/
return b ;

}

public static void main(String[] args){
Scanner in = new Scanner(System.in);
int a = 0 , b = 0 ;
a = in.nextInt();
b = in.nextInt();

int[] arr = parseBinary(a);
int[] brr = parseBinary(b);
int cnt = 0;//二进制数组中不同的位数
int up = 0 ;//遍历的上限，可以节省时间上的资源

for(int i = 0 ; i < 32 ; i++) {
if(Math.pow(2, i) > Math.max(a, b) && Math.pow(2, i-1) < Math.max(a, b)) {
up = i ;
break ;
}
}
for(int i = 31 ; i > 32-up-1 ; i--) {//遍历各有效位，不同位计数+1
if(arr[i] != brr[i]) {
cnt++;
}
}
System.out.println(cnt);

in.close();
}
}

```

```x, y = map(int, input().split(' '))
xor = x ^ y;
res = 0
while xor:
res += xor & 1 # 最后一位是不是 1
xor = xor >> 1 # 向右移
print(res)
```

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
String strX = to32string(x);
String strY = to32string(y);
int sum=0;
for(int i=0;i<strX.length();i++) {
if(strX.charAt(i)!=strY.charAt(i)) {
sum++;
}
}
System.out.println(sum);
}

public static String to32string(int x ) {
String strX = Integer.toBinaryString(x);
String ret = "";
for(int i=0;i<32-strX.length();i++) {
ret +="0";
}
return ret+strX;
}

public Main() {
super();
}

}

``````import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
in.close();
int ans = 0;
int nums = m ^ n; //相同的为0 不同的为1 异或
while(nums!=0){
nums &= (nums-1);
ans += 1;
}
System.out.println(ans);
}
}``````

#include <iostream>
using namespace std;
int main(void)
{
int m,n;
cin>>m,cin>>n;
int num=m^n;
size_t cont=0;
for(int i=0;i<32;i++)
if(1<<i&num)
cont++;
cout<<cont;
return 0;
}

```
import java.util.Scanner;

public class Main
{
public static int bitCount(int a, int b)
{
return Integer.bitCount(a^b);
}

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext())
{
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.print(bitCount(a,b));
}
}
}
```

20条回答 877浏览

# 通过挑战的用户

• 2019-07-23 19:27:04
• 2019-07-18 09:48:29
• 2019-07-17 20:41:29
• 2019-07-17 15:30:44
• 2019-07-15 17:07:49

# 相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题