The sword refers to offer - the number of 1s in binary

Preface: Because this topic requires the knowledge of in-place operations, we will briefly introduce lower-level operations.

Question :
    1. Why do all IT programmers respect bit operations so much?
    2. Why is the efficiency of bit operation higher than that of ordinary addition, subtraction, multiplication and division?

So, after reading these two questions, do you have the urge to learn about bit operations? Let's take a look at next-level operations.
Bit Operations : All numbers in a program are stored in binary form in the computer's memory . Bit operation, to put it bluntly, is to operate directly on the binary bits of integers in memory .
The operation mode of the computer is based on binary, so the decimal will be converted into binary and then operated, and the conversion process will reduce the running speed.

After reading the explanation of the above two terms, the reason for the high efficiency of bit operation is that the CPU of the computer directly uses the storage form (binary) of numbers in memory for calculation; and operations such as addition, subtraction, multiplication and division will first explain the symbols Meaning, and then calculate the result through the ALU (arithmetic logic unit), in this case, the efficiency is of course not as high as the bit operation.

Operators for bitwise operations:

and operation &
bitwise AND
or operation |
bitwise OR
xor operation ^
Bitwise XOR
not operation ~
bitwise negation
shl operation <<
shift left
shr operation >>
move right
Example one:
We can use the following code to calculate the parity of the number of 1s in the binary . When the binary representation of the input data has an even number of 1s, the program outputs 0 and an odd number of 1s. For example, if there are 9 1s in the binary 101000000111011011000 of 1314520, the program outputs 1 when x=1314520.
//c/c++ code
int even_ones (unsigned int x) {
x = x^(x >> 1); //(the last 1 digit of x)——represents the parity of the number of 1s in the first two digits of the original number
x = x^(x >> 2);     //(the last 1 digit of x)—— represents the parity of the number of 1s in the first four digits of the original number
x = x^(x >> 4);     //(the last 1 digit of x)—— represents the parity of the number of 1s in the first eight digits of the original number
x = x^(x >> 8);     //(the last 1 digit of x)—— indicates the parity of the number of 1s in the first sixteen digits of the original number
x = x^(x >> 16);     //(the last 1 digit of x)—— represents the parity of the number of 1s in the first 32 digits of the original number
return x & 1; // Take out the number represented by the last digit of x and return
}
explain:
The binary of 1314520 is 101000000111011011000. The result of the first execution of x=x^(x>>1) is as follows:
0000 0000 0001 0100 0000 1110 1101 1000
0000 0000 0000 1010 0000 0111 0110 1100(XOR)
---------------------------------------------------------------------
0000 0000 0001 1110 0000 1001 1011 0100
The result is a new binary number in which the ith bit from the right indicates whether there are an odd or even number of 1s in the ith and i+1 bits of the original number. For example, the 0 on the far right indicates that the last two digits of the original number have an even number of 1s, and the 1 in the third digit from the right indicates that there are an odd number of 1s in this position and the previous position of the original number.


Topic description

Input an integer and output the number of 1's in the binary representation of the number. Negative numbers are represented in complement.


Problem-solving idea: Do an AND operation with an integer and an integer minus 1, so that the rightmost 1 of the integer can be set to zero, and the previous part remains unchanged-according to this conclusion, how many times an integer can do this operation, just State how many 1s are in the binary of the integer.

Code: (C language implementation)

#include <stdio.h>

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

int main() {
	int num;
	int n;
	printf("Please enter an integer num: ");
	scanf("%d", &num);
	
	n = NumberOf1(num);
    	printf("Integer [%d] has [%d] 1's in binary", num, n);
        return 0;
 }

result:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721300&siteId=291194637