Find the number of ones in binary digits

Link to the original title: Niuke.com

Topic content:

Write a function that returns the number of 1s in the binary number of the parameter, and negative numbers are expressed in two's complement.

For example: 15 0000 1111 4 1

 

method one:

#include<stdio.h>

int NumberOf1(unsigned int n)
{
	int count = 0;
	while (n)
	{
		if (n % 2 == 1)
			count++;
		n /= 2;
	}
	return count;
}

int main()
{
	int n;
	scanf("%d", &n);
	int num = NumberOf1(n);
	printf("%d\n", num);
}

The implementation of the NumberOf1 function is relatively simple. It uses a loop to continuously divide n by 2 and judge whether the remainder is 1. If the remainder is 1, it means that the lowest bit in the binary representation of n is 1, and the counter count is incremented by 1. Then, n is shifted to the right by 1 bit, and the next cycle is continued until all bits in the binary representation of n have been accessed. Finally, the function returns the number of 1s.

In the main function, it calls the NumberOf1 function, reads in an integer n, and passes it as a parameter to the NumberOf1 function. The result returned by the function is assigned to the variable num, and the result is output to the console through the printf function.

Method Two:

#include<stdio.h>

int NumberOf1(int n)
{
	int i = 0, count = 0;
	for (i = 0; i < 32; i++)
	{
		if ((n >> i) & 1 == 1)
			count++;
	}
	return count;
}

int main()
{
	int n;
	scanf("%d", &n);
	int num = NumberOf1(n);
	printf("%d\n", num);
}

The implementation of the NumberOf1 function is relatively simple. It uses a for loop to check each bit in the binary representation of n. First, shift n to the right by i bits, and then use the bitwise AND operator (&) to determine whether the i-th bit of n is 1. If it is 1, add 1 to the counter count. Finally, after the loop is executed, the function returns the counted number of 1s.

It should be noted that when counting the number of 1s in the binary representation of signed integers, the influence of the sign bit should be considered. If the signed right shift operator (>>) is used, the sign bit is preserved. Therefore, the unsigned right shift operator (>>) can be used to remove the effect of the sign bit. In addition, you also need to be careful when using the bitwise operator (&) to determine whether a certain bit is 1.

Method three:

#include<stdio.h>

int NumberOf1(int n)
{
	int count = 0;
	while (n)
	{
		n = n & (n - 1);
		count++;
	}
	return count;
}

int main()
{
	int n;
	scanf("%d", &n);
	int num = NumberOf1(n);
	printf("%d\n", num);
}

The NumberOf1 function is implemented in a clever way, using a while loop and a trick called " Brian Kernighan's Algorithm ". In the loop, the bitwise AND operation is continuously performed on n and (n-1) , which will change the rightmost 1 in n to 0 . After each operation, the counter count increases by 1, and then proceeds to the next cycle, repeating this process until n is 0. Finally, the function returns the number of 1s.

Using Brian Kernighan's algorithm to count the number of 1s in the binary representation of an integer has a time complexity of O(log n), which is more efficient than other methods because it skips many unnecessary calculations. The algorithm is especially efficient when dealing with large datasets.

Guess you like

Origin blog.csdn.net/m0_73648729/article/details/130777500