0
点赞
收藏
分享

微信扫一扫

求二进制中不同位的个数

题目内容:

编程实现:两个 int (32位)整数m和n的二进制表达中,有多少个位(bit)不同?

输入例子:

1999  2299

输出例子:7

#include<stdio.h>
#include<string.h>
##define _CRT_SECURE_NO_WARNINGS1
int get_diff_bit(int m,int n,int count)
{
int tmp=m^n;//按位异或:相同为0,不同为1
while(tmp)
{
tmp=tmp&(tmp-1);//计算1的个数
count++;
}
return count;
}
int main()
{
int m=0;
int n=0;
scanf("%d%d",&m,&n);
int count=0;
count=get_diff_bit(m,n,count);
printf("count = %d\n",count);
return 0;
}


举报

相关推荐

LeetCode 二进制中1的个数

0 条评论