Bitwise operators: operations with, or, XOR, inversion, left-shift, and right-shift operations refer to binary operations. In system software, binary bitwise is often required. The C language provides six bitwise operators. These operators can only be used for integer operands, that is, they can only be used for signed or unsigned char, short, int, and long types. List of bitwise operators provided by C language: Operator description & bitwise AND if the two binary bits are both 1, The result value of this bit is 1, otherwise, the value is 0. | if one of the bitwise OR Two corresponding binary bits is 1, the result value of this bit is 1 ^ bitwise OR 0 if the two binary values involved in the operation are the same; otherwise, it is 1 ~ Reverse retrieval ~ It is a unary operator used to reverse a binary number by bit, that is, 0 is changed to 1, and 1 is changed to 0 <left shift is used to shift all the binary bits of a number to N places left, right fill 0> Right Shift shifts the binary digits of a number N places to the right, and is discarded when it moves to the low position on the right. For the unsigned number, the bitwise AND operator (&) of the bitwise AND operator (&) refers to the two data involved in the operation. The bitwise operation is performed on the bitwise AND operator. If both of the corresponding binary bits are 1, The result value of this bit is 1; otherwise, it is 0. Here, 1 can be interpreted as true in logic, and 0 can be interpreted as false in logic. The bitwise AND logic are consistent with the "and" operation rules. The logical "and" requires that the number of operations be true and the result is true. If A = true and B = true, then A bytes B = true. For example, the binary encoding of 3 & 5 3 is 11 (2 ). (In order to distinguish between decimal and other hexadecimal systems, this article stipulates that all non-decimal data is enclosed by brackets, which indicate the hexadecimal format in parentheses, And the binary value is marked as 2) the basic unit of memory data storage is Byte. A Byte is composed of eight bits. Bit is the minimum unit used to describe the data size of a computer. In a binary system, each 0 or 1 is a single bit. If 11 (2) is supplemented into one byte, It is 00000011 (2 ). The binary code of 5 is 101 (2). If it is supplemented into one byte, It is 00000101 (2) bitwise and operation: 00000011 (2) & 00000101 (2) 00000001 (2) from this we can see that 3 & 5 = 1c language code: # include <stdio. h> main () {int a = 3; int B = 5; printf ("% d", a & B) ;} (1) if you want to clear a storage unit, even if all its binary bits are 0, you only need to find a binary number, where each bit meets the following condition: the original number is 1, the corresponding digit in the new number is 0. Then perform the & Operation on the two to achieve the goal of clearing. For example, if the original number is 43, that is, 00101011 (2), find another number and set it to 148, that is, 10010100 (2), bitwise and calculation of the two: 00101011 (2) & amp; 10010100 (2) 00000000 (2) c source code: # include <stdio. h> main () {int a = 43; int B = 148; printf ("% d", a & B) ;}( 2) if you want to take the lower byte of an integer a (2 byte), you only need to bitwise a and 8 1. A 00101100 10101100b 00000000 111111c 00000000 10101100 (3) Retention refers to positioning: bitwise AND with a number, this number is 1. for example, if you want to retain the numbers 84, I .e. 01010100 (2) from the left, the numbers 3, 4, 5, 7, and 8, as follows: 01010100 (2) & 00111011 (2) 00010000 (2) that is: a = 84, B = 59 c = a & B = 16c source code: # include <stdio. h> main () {int a = 84; int B = 59; printf ("% d", a & B) ;}2. "bitwise OR" Operator (|) if one of the two binary bits is 1, The result value of this bits is 1. In logic, or operations are true. For example, 60 (8) | 17 (8), bitwise OR operation is performed on October 60 and October 17. 00110000 | 00001111001111c source code: # include <stdio. h> main () {int a = 060; int B = 017; printf ("% d", a | B);} application: bitwise OR operations are usually used to set a certain bit value to 1 for a data. For example, if you want to change the low 4 bits of a number to 1, you only need to perform bitwise OR operations on a and 17 (8. 3. Exchange two values without temporary variables such as a = 3, that is, 11 (2); B = 4, that is, 100 (2 ). To swap values of a and B, you can use the following assignment statement: a = a then B; B = B then a; a = a then B; a = 011 (2) (clerk) B = 100 (2) a = 111 (2) (result of a clerk B, a has changed to 7) (clerk) B = 100 (2) B = 011 (2) (B then a results, B has changed to 3) (clerk) a = 111 (2) a = 100 (2) (a then B results, a has been changed to 4) equivalent to the following two steps: ① execute the first two assignment statements: "a = a then B;" and "B = B then; "is equivalent to B = B Branch (a branch B ). ② Execute the third value assignment statement: a = a then B. Because the value of a is equal to (a then B) and the value of B is equal to (B then a then B), it is equivalent to a = a then B then a then B, that is, the value of a equals a between a and B between B and B. Amazing! C language source code: # include <stdio. h> main () {int a = 3; int B = 4; a = a ^ B; B = B ^ a; a = a ^ B; printf ("a = % d B = % d", a, B);} 4. Inverse Operator (~) It is a unary operator used to evaluate the binary anticode of an integer, that is, to change the value 1 of each binary bit of the operand to 0 to 1. Example :~ 77 (8) source code: # include <stdio. h> main () {int a = 077; printf ("% d ",~ A);} 5. The left shift operator (<) is used to shift the binary bits of a number to several bits left, the number of digits to be moved is specified by the right operand (the right operand must be a non-negative value). The number of digits to be moved to the right is 0, and the value of the left-shifted overflow value is discarded. For example, if the binary number of a is shifted to two places, the empty bit on the right is supplemented with 0, and the overflow bit on the left is discarded. If a = 15, that is, 00001111 (2), shifted to 2 places to 00111100 (2 ). Source code: # include <stdio. h> main () {int a = 15; printf ("% d", a <2);} shifts 1 to the left to multiply the number by 2, shifts the left two bits to multiply the number by 2*2 = <2 = 60, that is, 4. However, this conclusion is applicable only when the Left shift of the number is exceeded and the high value does not contain 1. Assume that an integer is stored in one byte (8 bits). If expression a is an unsigned integer variable, the overflow is 0 when expression a = 64 and expression a shifts one bit left, when the Left shift is two places, the overflow high contains 1. 6. the right-shift operator (>) the right-shift operator is used to shift the binary digits of a number several places to the right. The number of digits to be moved is specified by the right-hand operand (the right-hand operand must be non-negative ), the low position moved to the right side is discarded. For the unsigned number, the high position is supplemented with 0. For a signed number, some machines fill the blank part on the left with a signed bit (that is, "arithmetic shift "), some other machines fill the left blank part with 0 (that is, "logical shift "). NOTE: For the unsigned number, when the right shift is performed, the Left high is moved to 0. For the signed value, if the original sign bit is 0 (the number is positive), the left is also moved to 0. If the original symbol bit is 1 (negative), the left side is moved to 0 or 1, depending on the computer system used. Some systems move to 0, and some systems move to 1. Moving 0 is called "logical shift", that is, simple shift; moving 1 is called "arithmetic shift ". For example, the value of a is octal 113755: a: 1001011111101101 (expressed in binary form) a> 1: 0100101111110110 (logical right shift) a> 1: 1100101111110110 (arithmetic right shift) in some systems, a> 1 gets 045766 bits, and 145766 may be obtained in other systems. Turbo C and some other C compilers use the arithmetic right shift, that is, when the number of symbols is shifted to the right, if the original symbol bit is 1, the left side is shifted to 1. Source code: # include <stdio. h> main () {int a = 0113755; printf ("% d", a> 1 );} 7. bitwise assignment operators bit operators and assignment operators can form a composite assignment operator. Example: & =, |=, >>=, <=, region = example: a & = B is equivalent to a = a & B a <= 2 is equivalent to a = a <2