Today, we will learn about the lower bit operators .

Today we should also study in a happy mood !

<>1. What operators are there ? How to use it ?

& Bitwise AND : Bitwise operation with 1 by 1, Others are 0
| Bitwise OR : Bitwise operation with 0 by 0, Others are 1
^ Bitwise XOR : Bitwise operation The difference is 1, Same as 0
~ Reverse : Bitwise operation Bitwise inversion ( unary operator )
<< Shift left : Bit by bit left shift least bit complement 0
>> Shift right : Shift right bit by bit with sign to fill the highest sign bit , Unsigned complement 0.
except ~ Reverse , The others are binocular operators .

<>2. What data types do bit operators apply to ?

The answer is only for plastic families , Not suitable for floating point family .
The essential reason is that the data storage types of the two are different .
The storage of the plastic family was mentioned in the previous article , No more details .

* Shaping family storage
Storage mode of floating point family , To comply with IE754 Provisions of .
<>3. Bitwise AND Bitwise OR Bitwise XOR Instance of

<> example 1: Find binary middle 1 Number of . adopt &( Bitwise AND )
int a = 0; scanf("%d", &a); int count = 0; while (a) { a = a&(a - 1);
// Discard the lowest bit each time , until a by 0. count++; } printf("%d\n", count);
<> example 2: Find binary middle 0 Number of adopt | ( Bitwise OR ),( Very interesting , You can have 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 , The judgment condition is established by truncating after data overflow .
first , Used in judgment a+1 Because to make 0 Go in, too , More coincidentally , When a+1 equal to 0 When , just a yes -1, We know -1 Is not in the binary complement of 0 of
, So it just fits , This is used to change one bit at a time into 1, Make the data negative in the end (-1), Add 1 Become 0, Terminate cycle .

<> example 3: adopt ^ ( Bitwise XOR ) Swap the values of two variables .

First of all, we need to know some basic conclusions :
1 . A number exclusive or 0, Or is it itself .
2. A number XOR is itself 0
This is based on the XOR concept .

Let's look at this question again , Have you got some ideas .
before a XOR b, And then XOR again , Equivalent to b = b^ b^a.
By the same token a.
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);
Here's 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 XOR method over mathematical method is that it does not need to consider the problem of overflow , If two very large numbers are operated mathematically, they may overflow .

<>4. Shift left Shift right operator

Shift left is simple , Discard high bit , Low complement 0.
Right shift sub case discussed :
Arithmetic shift right : For signed numbers , Highest complement sign bit .
Logical shift right : For unsigned numbers , Highest complement 0.

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

Found after operation , First printed is -1, And then you print 230-1.
Analyze first a>>1, because a Is a signed negative number , So the highest sign bit during filling 1, The last complement value is all 1.
Look again b>>1, because b Is an unsigned number , So the highest position is filled 0, Finally, the first bit out is 0, Others are 1.

So what is the scope of the shift ?
first int Type data only 32 position , So the maximum shift is 31 position , What about the minimum value ? Yes minimum yes **-31 Seats ?** No , Neither shift left nor shift right can shift negative digits .
So the range of the shift is 0~31 position .

Take an example
Compare lower priority issues .
int a = 1; a = a << 2 + 3; printf("%d", a);
first , The result is 32, So if you look at it this way , Yes, first (2+3), Shift in progress .
So when using the shift character , Questions to consider priority .

<>5. Shift character application

Using the shift character, the bit by bit position can be 0 or 1.
Bit by bit , Set 1.
unsigned int a = 1; int i = 0; for (i = 0; i < 32; i++) { a = a|(a << i); }
printf("%d", a);
Because the last one is signed , So print as -1.

Bit by bit , Set 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 , So when filling , repair 0, Perform bitwise or after shifting . Last printed yes 0.

<>6. Some skills

* x&1 == 0 Judge whether the number is even , The last bit of the even number is 0.
* The values of two numbers can be exchanged by XOR .
* x & (x-1) Can make the 1 Become 0.
* For positive numbers ,x&(x-1)== 0 Is to judge 2 Power of .
* Two identical numbers exclusive or , The result is 0, A sum of numbers 0 XOR , The result is himself . Can be used to find numbers .
<>7. Forecast for next period

I still haven't ++ I've finished writing my stuff . In the next issue ++ and – Relevant knowledge .
The next issue is more exciting ~~~

Technology
Daily Recommendation