首页 > 编程语言 > 详细

【C语言】求两个数中不同的位的个数

时间:2017-05-10 09:17:12      阅读:224      评论:0      收藏:0      [点我收藏+]
//求两个数中不同的位的个数
#include <stdio.h>
int count_different(int a, int b)
{
	int count = 0;
	int c = a^b;        //a,b中不同的位即为1
	while (c)
	{
		count++;
		c = c&(c - 1);  //把c中最后一个1去掉
	}
	return count;
}
int main()
{
	printf("%d\n", count_different(3,8));   //3
	printf("%d\n", count_different(0, 6));  //2
	printf("%d\n", count_different(-1,1));  //31
	return 0;
}

【C语言】求两个数中不同的位的个数

原文:http://www.cnblogs.com/lytwajue/p/6834228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!