Detailed explanation of bit operators, and some bit operation tips

Learn about bitwise operators today.

insert image description here

Have fun learning today!

1. What are the operators? How to use it?

& Bitwise AND: Bitwise operation Same as 1 , others are 0
| Bitwise OR: Bitwise operation Same as 0 , others are 1
^ Bitwise XOR: Bitwise operation Difference is 1, same 0
~ Negation: bit- by Bit-wise inversion (unary operator)
<< Left shift: Left shift left bit by bit to complement 0
>> Right shift: Bit-by-bit right shift signed to complement the highest bit Sign bit, fill 0 for unsigned numbers .
Except for the negation of ~, everything else is a binary operator.

2. What data types are bitwise operators applicable to?

The answer is that it only applies to the integer family, not the floating-point family.
The essential reason is because the data storage types of the two are different.
The storage of the plastic family has been mentioned in the previous article, so I will not repeat it.

  • The storage method of the integer family
    The storage method of the floating-point family must comply with the provisions of IE754.

3. Example of bitwise and bitwise or bitwise XOR

Example 1: Find the number of 1s in binary. Take & (bitwise AND)

    int a = 0;
	scanf("%d", &a);
	int count = 0;
	while (a)
	{
    
    
		a = a&(a - 1);//每次把最低位丢弃,直到a为0.
		count++;
	}
	printf("%d\n", count);

Example 2: Find the number of 0s in binary using | (bitwise OR), (very interesting, you can take a look)

	int a = 0;
	int count = 0;
	scanf("%d", &a);
 	while (a+1)
	{
    
    
		a = a | (a + 1);
		count++;
	}
	printf("%d\n", count);

This code is very interesting, it uses truncation after data overflow to make the judgment condition true.
First of all, a+1 is used in the judgment because 0 needs to be included. More coincidentally, when a+1 is equal to 0, it happens that a is -1. We know that there is no 0 in the two's complement of -1. , so it just matches. This is used to make one bit become 1 at a time, and finally make the data become negative (-1), add 1 to become 0, and terminate the loop.

Example 3: Use ^ (bitwise exclusive OR) to swap the values ​​of two variables.

First of all to know some of the most basic conclusions:
1. A number XOR 0, or itself.
2. A number XOR itself is 0
This is derived from the XOR concept.

Let's look at this question again, is there any idea?
First a XOR b, and then perform an XOR, which is equivalent to b = b^ b^a.
Similarly, a can be obtained.

    int a = 10;
	int b = 20;
	printf("before:%d %d\n", a, b);
	a = a^b;
	b = a^b;
	a = a^b;
	printf("after:%d %d\n", a, b);

Below is the math.

    int a = 10;
	int b = 20;
	printf("before:%d %d\n", a, b);
	a = a + b;
	b = a - b;
	a = a - b;
	printf("after:%d %d\n", a, b);

The biggest advantage of the XOR method over the mathematical method is that there is no need to consider the overflow problem. If two very large numbers are mathematically operated, the overflow may occur.

4. Left shift right shift operator

Shifting to the left is relatively simple, discarding the high bits and filling the low bits with 0.
The case of right shift is discussed:
arithmetic right shift: for signed numbers, the highest bit is supplemented by the sign bit.
Logical right shift: For unsigned numbers, the highest bit is filled with 0.

See an example:

    int a = -1;
	unsigned int b = -1;
	printf("%d\n", a >> 1);
	printf("%u\n", b >> 1);

insert image description here
After running, it is found that -1 is printed first, and then 2 30 -1 is printed.
First analyze a>>1, because a is a signed negative number, so the highest bit sign is 1 when filling, and the last complement The value is still all 1.
Look at b>>1, because b is an unsigned number, so the highest bit is filled with 0 when filling bits, and finally the first bit is 0, and the others are 1.

So what is the range of the shift?
First of all, the data of int type has only 32 bits, so the maximum shift is 31 bits, so what about the minimum value? Is the minimum value is **-31 bits? **No, neither left nor right shift can shift negative bits.
So the shift range is 0~31 bits.

Let's see an example
of a prioritization problem.

    int a = 1;
	a = a << 2 + 3;
	printf("%d", a);

First of all, the result is 32, so if you look at it this way, it is (2+3) first, and then the shift is performed.
Therefore, when using the shift character, we must consider the issue of priority.

5. Shift operator application

Bit-by-bit position can be set to 0 or 1 using the shift operator
. Bit-by-bit, set to 1.

    unsigned int a = 1;
	int i = 0;
	for (i = 0; i < 32; i++)
	{
    
    
		a = a|(a << i);
	}
	printf("%d", a);

Because the last is a signed number, it is printed as -1.

bit by bit, set to 0

    unsigned int a = -1;
	for (int i = 0; i < 32; i++)
	{
    
    
		a = a & (a >> i);

	}
	printf("%d", a);

Because a is an unsigned number, when complementing bits, 0 is added, and bitwise OR is performed after shifting. The last print is 0.

6. Some tips

  1. x&1 == 0 to judge whether it is even, the last bit of the even number is 0.
  2. The values ​​of two numbers can be exchanged by XOR.
  3. x & (x-1) can make a 1 on the rightmost bit become a 0.
  4. For positive numbers, x&(x-1)==0 is to judge whether it is a power of 2.
  5. XOR two identical numbers, the result is 0, and XOR a number with 0, the result is itself. Can be used to find numbers.

7. The next notice

This issue still hasn't finished writing the ++ stuff. In the next issue, I will talk about ++ and – related knowledge.
The next issue will be more exciting~~~
insert image description here

Guess you like

Origin blog.csdn.net/m0_64770095/article/details/124022347