Count the number of 1s in a number binary

code directly

method one

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int count_1(unsigned int a)//unsigned int正负数通用
{
	int count = 0;
	while (a)
	{
		if (a % 2 == 1)
		{
			count++;
		}
		a = a / 2;//eg:输入:16/2-->8/2-->4/2-->2/2-->1%2=1(count=1)-->1/2=0(退出循环)
	}
	return count;
}
int main()
{
	int a = 0;
	scanf("%d", &a);
	int ret = count_1(a);
	printf("%d\n", ret);
	return 0;
}

Method Two

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
方法二
int count_1(int m)
{
	int c = 0;//统计1的个数
	int i = 0;
	for (i = 0; i < 32; i++)
	{
		if ((m & 1) == 1)//判断二进制最后一位是不是1
		{
			c++;
		}
		m >>= 1;//由于右移后是在左边补符号位(vs编译器),不会影响&的运算
	}
	return c;
}
int main()
{
	int a = 0;
	scanf("%d", &a);
	int ret = count_1(a);
	printf("%d\n", ret);
	return 0;
}

Notice

1. About the right shift operator:

1 Arithmetic right shift (discard the right, fill the original sign bit (1/0) on the left)

2 Logical shift right (discard on the right, fill 0 on the left)

Existing that depends on the compiler (vs is arithmetic right shift)

two

For shift operators, do not shift negative bits, this is undefined by the standard.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//方法三
int count_1(int m)
{
	int c = 0;
	while (m)
	{
		m = m&(m - 1);
		c++;

	}
	return c;
}


int main()
{
	int a = 0;
	scanf("%d", &a);
	int ret = count_1(a);
	printf("%d\n", ret);
	return 0;
}
//eg:m=16
m             001111(补码后几位)
m-1           001110
m=m&(m-1)     001110
m-1           001101
m=m&(m-1)    001100
m-1           001011
m=m&(m-1)     001000
m-1           000111
m             000000//到0之前总共变了四次

at the same time

(m&(m-1))==0 can also judge that m is 2^k (there is only one 1 in the binary bit)

Guess you like

Origin blog.csdn.net/weixin_63451038/article/details/121594398