首页 > 试题广场 >

bit位数

[编程题]bit位数
  • 热度指数:2707 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
两个整数 m 和 n 的二进制表达中有多少个位(bit)不同?

数据范围:

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


输出描述:
输出这两个数字中bit不同的个数
示例1

输入

15 8

输出

3

说明

15的二进制表示中后四位是 1111 , 3的二进制表示中后四位是 0011,因此有两位不同 
#include <stdio.h>
#include <stdlib.h>
int main()
{
    unsigned int x,y;
    while(scanf("%d %d",&x,&y)!=EOF)
    {
        unsigned int temp=x^y;
        int count=0;
        while(temp)
        {
            if(temp%2)
            {
                count++;
            }
            temp/=2;
        }
        printf("%d\n",count);
    }
    return 0;
}

发表于 2021-08-20 21:25:00 回复(0)
#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int m, n;
  fscanf(stdin, "%d %d", &m, &n);
  return fprintf(stdout, "%d", __builtin_popcount(m ^ n)), 0;
}

发表于 2021-07-24 10:42:18 回复(0)

问题信息

上传者:小小
难度:
3条回答 3676浏览

热门推荐

通过挑战的用户

查看代码