bit arithmetic
The operation component of a bitwise operation can only be integer or character data, and the bitwise operation considers the operand as a bit string information composed of binary bits, and completes the specified operation by bit, and obtains the result of the bit string information. The bitwise operators are:
& (Bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise reversed). Where the bitwise negation operator is the monocular operator and the rest is the binocular operator.
The bitwise operators take precedence from high to low, in the order of ~, &, ^, |,where ~ 's binding direction is from right to left, and precedence is higher than arithmetic operators, the rest of the operators are bound from left to right, and the precedence is lower than the relational operator.
(1) Bitwise AND operator (&)Bitwise-and-arithmetic computes the corresponding bits of the two operand components according to the following rules: 0 & 0 = 0, 0 & 1 = 0, 1 & 0 = 0, 1 & 1 = 1. That is, the same as 1 bit, the result is 1, otherwise the result is 0. For example, a 3 internal representation of 00000011 5 internally expressed as 00000101 3&5 results in 00000001 bitwise AND operations have two typical uses, one is to take a bit string information of some bits, such as the following code intercept x The minimum 7 bits: X & 0177. The other is to let a certain variable hold a certain number of bits, the remaining position 0, such as the following code to keep x only the lowest 6 bits: x = x & 077. The above use is to design the good one constant, the constant only the required bit is 1, the unwanted bit is 0. Use it to bitwise with the specified bit string information.
(2) Bitwise OR operator (|)Bitwise OR operation calculates the corresponding bits of the two operation components according to the following rules: 0 | 0 = 0, 0 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1 that is, as long as there is a bit 1, the result is 1, otherwise 0. For example, 023 | 035 The result is 037. A typical use of a bitwise OR operation is to place a number of bits of information in a bit string into 1. If you are going to get the right 4 to 1, the other bits are the same as the other bits of the variable J, available in logic or Operation 017|j. To assign this result to the variable J, it can be written as: j = 017|j
(3) Bitwise XOR operator (^)Bitwise XOR computes the corresponding bits of the two operand components according to the following rules: 0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0 that is the same value for the corresponding bit, the result is 0, and the result is 1. For example, the 013^035 result is 026. The meaning of the XOR operation is to find out whether the corresponding bit value of the two operation components is different, the difference is 1, the same is 0. A typical use of bitwise XOR is to find the inverse of a certain number of bits of information in a bit string. If the desired integer variable J of the right 4 bits of the inverse, with a logical XOR operation 017^j, can be obtained J Right 4 bits of information, that is, the original 1 bit, the result is 0, the original 0 bit, the result is 1.
(4) Bitwise inverse operator (~)The bitwise negation is the monocular operation, which is used to find the inverse of a bit string information, that is, which is 0 bits, the result is 1, and which is 1 bits, the result is 0. For example, the result of the 0XFFF8 is. Inverse operations are often used to generate constants unrelated to the implementation of the system. To make the variable x 6 position 0, the remaining bits will be the same, the code x = x & ~077 can be implemented. The above code is independent of the integer x with 2 bytes or 4 byte implementations. When two different lengths of data are performed for bitwise operations (for example, Long data and int type data), the right end of the two operational Components is aligned for bitwise operation. If the short number is positive, the high is filled with 0, and if the short number is negative, the high position is filled with 1. If the short is an unsigned integer, the high level is always filled with 0. Bit operation is used to compute the bit string information and get the result of bit string information. The following code can remove the bit string information of the integer variable K with the rightmost 1 information bit: ((k-1) ^k) & K.
Shift OperationsThe shift operation is used to
integral movement of integer or character data as a binary information string。 There are two operators:
<< (shift left) and >> (shift right)The shift operation is binocular operation, there are two operational components, the left component is the shift data object, the right component value is the shift bit number. The shift operation regards the left operation component as a bit string information composed of binary, and shifts it to the left or right to obtain a new bit string information. The shift operators have precedence over the arithmetic operators, which are higher than the relational operators, and are bound from left to right.
(1) left shift operator (<<)The left-shift operation shifts a bit string information to the left of the specified bit, and the right-side vacated bits are supplemented with 0. For example 014<<2, the result is 060, or 48. When you move left, the right end of the empty is supplemented with 0, and the information of the bits moved out is discarded. In a binary number operation, if the information is not lost due to movement, the 1 bits per left shift is equivalent to multiply by 2. such as 4 << 2, the result is 16.
(2) Right shift operator (>>)The right-shift operation shifts a bit string information to the right of the specified bit, and the information for the bits moved out of the right side is discarded. For example 12>>2, the result is 3. In contrast to the left shift, for small integers, each right shifts 1 bits, which is equivalent to dividing by 2. When you move right, you need to be aware of the sign bit problem. For unsigned data, when moving right, the left-side vacated bits are supplemented with 0. For signed data, if the pre-shift sign bit is 0 (positive), the left-hand is also supplemented with 0, and if the pre-shift sign bit is 1 (negative), the left-hand is 0 or 1 supplemented, depending on the computer system. For negative right shift, the system called 0 complements is "logical right Shift", and the system with 1 complements is "arithmetic right shift". The following code illustrates the right-shift approach to the system used by the reader: printf ("%d\n\n\n", -2>>4); If the output is-1, the arithmetic is shifted to the right, and the output is a large integer, then the logical right shift. The combination of shift operation and bitwise operation can realize many complex computations related to bit string operation. The position of the variable is numbered from right to left, from 0 bits to 15 bits, and the expression referring to the position is a positive integer not exceeding 15. The following code has the meanings shown in the comments on the right:
~ (~0 << N)
(x >> (1 p-n)) & ~ (~0 << N)
New |= ((Old >> Row) & 1) << (15–k)
s &= ~ (1 << j)
For (j = 0; ((1 << J) & s) = = 0; j);===================================================================================================
A bitwise operation is a binary-based operation. In system software, it is often necessary to deal with bits problems. The C language provides 6 bit manipulation operators. These operators can only be used for integer operands, that is, only for signed or unsigned char,short,int and long types.
The C language provides a list of bit operators : operator meaning description & bitwise AND if two corresponding bits are 1, then the result value of this bit is 1, otherwise 0 | In a bitwise or two corresponding bits, as long as there is a value of 1, the result of the bit is 1 ^ bitwise XOR or if the two bits value of the operation is equal to 0, otherwise 1 ~ negation ~ is a unary operator, which is used to reverse the bitwise negation of a binary number, 0 to 1, 1 to 0 << left Used to move a number of each bits all left n bits, right 0 >> right shift a number of each bits right shift n bits, moved to the right side of the low is discarded, for unsigned number, high 0
1, bitwise-AND Operator (&) bitwise-and-refers: two data of the participating operation, and the "and" operation by bits. If two corresponding bits are 1, the result value of the bit is 1; otherwise 0. The 1 here can be understood as the logic in which the true,0 can be interpreted as false in logic. Bitwise AND is actually the same as the logical "and" arithmetic rules. The logic of "and", the demand for the operation of the whole truth, the result is true. If, a=true,b=true, A∩b=true for example: 3&5 3 is a binary code of 11 (2). (in order to differentiate between decimal and other binaries, this article stipulates that all non-decimal data are appended with parentheses after the data, annotated with the binary in parentheses, and binary labeled as 2). The basic unit of memory storage data is byte (byte), and a byte consists of 8 bits (bit). Bits are the smallest unit used to describe the amount of computer data. In a binary system, each 0 or 1 is a bit. 11 (2) is made up to one byte, then 00000011 (2). The binary encoding of 5 is 101 (2), which complements it to one byte, then 00000101 (2) Bitwise AND Operation: 00000011 (2) &00000101 (2) 00000001 (2) 3&5=1 C language code: #include <stdio.h> Main () {int a=3; int b = 5; printf ("%d", a&b);} Bitwise AND use: (1) Clear 0 if you want to clear a storage unit, even if it is all bits 0, just find a binary number, where each bit Meet the criteria:
The original number is 1 bits, and the corresponding bit in the new number is 0. Then make the two & operations, you can achieve the goal of clear 0. Example: The original number is 43, that is 00101011 (2), another number, set it to 148, that is, 10010100 (2), the two bitwise AND operation: 00101011 (2) &10010100 (2) 00000000 (2) C language source code: #include <stdio.h> Main () {int a=43; int b = 148; printf ("%d", A&b);} (2) Take a number of some of the positioning if there is an integer a (2byte), want to take the low byte, only need to be a and 8 1 bitwise and can. A 00101100 10101100 B 00000000 11111111 C 00000000 10101100 (3) reserved refers to positioning: with a number of "bitwise with" operation, this number in that bit takes 1. For example: There is a number 84, or 01010100 (2), which would like to keep the 3,4,5,7,8 bit from the left, the operation is as follows: 01010100 (2) &00111011 (2) 00010000 (2) namely: a=84,b=59 c=a& B=16 C Language Source code: #include <stdio.h> main () {int a=84; int b = n; printf ("%d", a&b);}
2, the bitwise OR operator (|) two corresponding bits, as long as there is a value of 1, the result of the bit is 1. In the case of logic or arithmetic, one is true.
。 For example: 60 (8) |17 (8), octal 60 and octal 17 are bitwise OR operations. 00110000 |00001111 00111111 C language source code: #include <stdio.h> main () {int a=060; int b = 017; printf ("%d", a|b);} application: Bitwise OR operation It is often used to set a value of 1 for a certain bit of data. For example, if you want to change the lower 4 bits of a number A to 1, you only need to make a and 17 (8) bitwise OR operations.
3, Exchange two values, without temporary variables such as: A=3, namely one (2); b=4, or 100 (2). To swap the values of a and B, you can use the following assignment statement: a=a∧b; B=b∧a; A=a∧b; a=011 (2) (∧) b=100 (2) a=111 (2) (Result of A∧b, A has become 7) (∧) b=100 (2) b=011 (2) (Result of B∧a, B has become 3) (∧) a=111 (2)
A=100 (2) (as a result of A∧b, A has become 4) is equivalent to the following two steps: ① executes the first two assignment statements: "A=A∧B;" and "b=b∧a;" equivalent to B=b∧ (A∧B). ② executes a third assignment statement: A=a∧b. Since the value of a is equal to (A∧B), the value of B is equal to (B∧A∧B),
Thus, the equivalent of A=a∧b∧b∧a∧b, that is, the value of a equals a∧a∧b∧b∧b, equals B. It's 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, "negation" operator (~) He is a unary operator, which is used to find the binary inverse of integers, i.e. 1 of each bits on the operand is changed to 0, and 0 becomes 1. Example: ~77 (8) Source code: #include <stdio.h> main () {int a=077; printf ("%d", ~a);}
5. Left shift operator (<<)
The left shift operator is used to shift the bits of a number to the left by several bits, the number of bits moved is specified by the right operand (the right operand must be a non-negative
Value), the right-hand vacated bits are filled with 0, and the high-left-shift overflow is discarded. For example: The binary number of a is shifted to the left 2 bits, the right side vacated the bit 0, left overflow bit discarded. If a=15, i.e. 00001111 (2), shift left 2
The position was 00111100 (2). Source code: #include <stdio.h> main () {int a=15; printf ("%d", a<<2), left 1-bit equivalent to this number multiplied by 2, left 2-bit equal to the number multiplied by 2*2=4,15<<2=60, that is, by 4. However, this conclusion applies only to the
A number that is discarded when the left shift is dropped does not contain a 1 case. Suppose that an integer is stored in one byte (8 bits), and if A is an unsigned integer variable, then when a=64, the left one is overrun by 0.
, while moving the left 2 bits, the overflow high has 1.
6, right shift operator (>>) right shift operator is used to move a number of bits to the right of several bits, the number of bits moved by the right operand specified (right operand must be non-negative
Value), move to the right end of the low is discarded, for unsigned number, high 0. For signed numbers, some machines will be left blank
Fill with the sign bit (i.e. "arithmetic shift"), while others fill the left empty part with 0 (i.e. "logical shift"). Note
Meaning: For unsigned numbers, the left-hand side moves to 0 when you move right, and if the original sign bit is 0 (positive) for signed values, the left side is also shifted
Into the 0. If the sign bit turns out to be 1 (that is, negative), the left-hand side is 0 or 1, depending on the computer system used. Some systems move into 0, some
The system moves into 1. Moving into 0 is called "logical shift", i.e. simple shift, and moving into 1 is called "Arithmetic shift". Example: The value of A is an octal number 113755:a:1001011111101101 (represented in binary form) a>>1:0100101111110110 (when the logical right shift) a>>1:11001011111101 10 (arithmetic right shift) in some systems, a>>1 have octal numbers of 045766, while on other systems it is possible to get 145766. Turbo C and some other C
The compilation takes the arithmetic right shift, that is, when the signed number is shifted to the right, if the sign bit is 1, the left side of the high is 1. Source code: #include <stdio.h> main () {int a=0113755; printf ("%d", a>>1);}
7, bitwise operation Assignment operator
The bitwise operator and the assignment operator can form compound assignment operators. Example: &=, |=, >>=, <<=,∧= example: A & = b equals A = A & B a << = 2 corresponds to a = a << 2
Transferred from: http://blog.sina.com.cn/s/blog_60e96a410100mjd2.html
Bit operations in C/C + +