Detailed explanation of bit operators (C language)

Preface
The bitwise operators in C language are operators used to perform bit-level operations on the binary representation of data. These operators include bitwise AND (&), bitwise OR (|), bitwise XOR (^), and bitwise negation (~). These bitwise operators can be used to perform various bit-level operations, such as obtaining binary representation. Specific bits, set specific bits to 0 or 1, swap values ​​of variables, etc. They are frequently used in low-level programming, bit-level coding, and optimization algorithms. Today I will share with you the use of bit operators.
Insert image description here

Article directory

1,&

& is a bitwise AND operator that can perform a bitwise AND operation on the binary representation of two operands. First of all, we all know that binary only has the numbers 0 and 1, so how does & operate?
Let's take an example. Below are the binary representations of 8 and 11.
8: 1000
11:1011

& is called bitwise AND, so we have to perform the AND operation according to the corresponding positions of the two binary numbers. For example, 1 of 8 corresponds to 11 of 11, 0 corresponds to 0, 0 corresponds to 1, and 0 corresponds to 1 (I am from The order from front to back is easy to understand).
When a certain bit is both 1, the result after the AND operation is 1. When one side is 1 and the other side is 0, the result is 0. When both sides are 0, the result is still 0. We can imagine it as a series circuit. , the switch is 0 and 1, and when it is 1, the switch is closed so that the current can pass.
Insert image description here

So the result of 8&11 is 1000 which is 8

2,|

| is a bitwise OR operator, similar to &. Let’s use 8 and 11 as an example:
8: 1000
11:1011

When both sides of a certain bit are 1, the result is 1. When one side is 1 and the other side is 0, The result is 1. When both sides are 0, the result is 0. We can imagine this as a parallel circuit. As long as a switch is turned on, the current can flow.
Insert image description here

So the result of 8|11 is 1011 which is 11

3,^

^ is the bitwise XOR operator. We still use 8 and 11 as an example.
8: 1000
11:1011

When both sides of a certain bit are 1 or 0, the result is 0. When both sides are different (that is, one is 1 and the other is 0), the result is 1. That is, when a certain bit is the same, the result is 0, and when it is different, the result is 1.
Therefore, the result of 8&11 is 0011, that is, the result is 3.

4,~

~ is the bitwise negation operator, take 1 and -1 as an example.
1: 00000000 00000000 00000000 00000001
-1:111111111 11111111 11111111 11111111

The inversion means that 0 becomes 1 and 1 becomes 0, so the bitwise inversion of 1 and -1 will respectively become:
1: 11111111 1111111 1 11111111 11111110
-1: 00000000 00000000 00000000 00000000

Then we can find that 1 = (-1) + 1, -1 = (1) + 1, so that we can easily convert positive numbers and negative numbers.

end

That’s it for sharing about bit operators. If you think the blogger’s talk is good, don’t forget to give the blogger a follow, like, and favorite~, friends, see you in the next issue!

Guess you like

Origin blog.csdn.net/Tokai___Teio/article/details/135113625