博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1.输出一个整数的每一位。2. 编程实现: 两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?
阅读量:4116 次
发布时间:2019-05-25

本文共 637 字,大约阅读时间需要 2 分钟。

1.从最高位开始依次输出:把1左移31位,就是最高比特位,再&1,进行判断0或1;循环直到最低位。

#include
#include
int showDataBits(int x){ int i = 31; for (i = 31; i>=0; i--) { if (x&(1<

   结果是: 

2.通过异或^求出两个数中的不同数,然后计数有多少位不同。输入例子: 1999 2299 。输出结果为:7.

int main(){	int a = 1999;	int b = 2299;	int count = 0;	int num= a^b;//相异为1,相同为0。求出ab中不同位。	while( num)	{		count++;		num = num&(num - 1);//统计1的位数。	}	printf("%d\n", count);	system("pause");	return 0;}

通过函数实现:

#include
#include
int countBit(int i){ int n = 0; while (i){ i = i&(i - 1); n++; } return n;}int main(){ int n=countBit(1999 ^ 2299); printf("%d", n); system("pause"); return 0;}

你可能感兴趣的文章
VUe+webpack构建单页router应用(一)
查看>>
Node.js-模块和包
查看>>
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
PHP 7 的五大新特性
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
OpenCV meanshift目标跟踪总结
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
听说玩这些游戏能提升编程能力?
查看>>
如果你还不了解 RTC,那我强烈建议你看看这个!
查看>>
沙雕程序员在无聊的时候,都搞出了哪些好玩的小玩意...
查看>>
Mysql复制表以及复制数据库
查看>>
Linux系统编程——线程池
查看>>
Linux C++线程池实例
查看>>
shared_ptr的一些尴尬
查看>>
C++总结8——shared_ptr和weak_ptr智能指针
查看>>
c++写时拷贝1
查看>>