Bitwise operations are operations that are performed in binary order. In system software, it is often necessary to deal with bits problems. The C language provides 6 bit operator operators. These operators can only be used for integer operands, that is, only for signed or unsigned char,short,int and long types.
The list of bitwise operators provided by the C language:
Description of operator meaning
& Bitwise AND if two corresponding bits are 1, the result value of this bit is 1, otherwise 0
| A bitwise or two corresponding bits as long as one is 1, the result value of this bit is 1
^ Bitwise XOR or if the two bits values are equal to 0, otherwise 1
~ is a unary operator, which is used to reverse a binary number by a bit, which is about 0 to 1, and 1 to 0.
<< left to move a number of bits all left n bit, right 0
>> Right Move a number of each bits to the right n bit, move to the right side of the low is discarded, for unsigned number, high 0
1, "Bitwise AND" operator (&)
Bitwise vs. refers to: Two data that participates in the operation, and the "and" operation is done 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 true,0 in logic that can be interpreted as false in logic. Bitwise AND is in fact consistent with the logical "and" Operation rules. The logic of "and", the requirements of the full truth, the results are true. If, a=true,b=true, then a∩b=true for example: 3&5 3 binary encoding is 11 (2). (to differentiate between decimal and other systems, this article stipulates that all non-decimal data is appended to the data followed by parentheses, which are marked in parentheses, and binary is labeled 2 the base unit of the memory-stored data is byte (byte) and a byte consists of 8 bits (bit). A bit is the smallest unit used to describe the amount of data in a computer. In binary systems, each 0 or 1 is a bit. The 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)
So 3&5=1
C Language code:
Copy Code code as follows:
#include <stdio.h>
Main ()
{
int a=3;
int b = 5;
printf ("%d", a&b);
}
Bitwise AND PURPOSE:
(1) Clear Zero
If you want to clear zero for a storage unit, even if all of its bits is 0, just find a binary number where each bit meets the criteria:
The original number is 1 bit, the corresponding bit in the new number is 0. Then make the two & operations, you can achieve the purpose of the Qing 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:
Copy Code code as follows:
#include <stdio.h>
Main ()
{
int a=43;
int B = 148;
printf ("%d", a&b);
}
(2) To take certain points in a number of positions
If you have an integer a (2byte), you want to take the lower byte, you only need to put a and 8 1 bitwise and can.
A 00101100 10101100
B 00000000 11111111
C 00000000 10101100
(3) retention refers to positioning:
With a number of "bitwise AND" operations, this number is 1 in that bit.
For example, a number of 84, or 01010100 (2), would like to retain the first 3,4,5,7,8 bit from the left, as follows:
01010100 (2)
&00111011 (2)
00010000 (2)
namely: a=84,b=59
C=a&b=16
C Language Source code:
Copy Code code as follows:
#include <stdio.h>
Main ()
{
int a=84;
int B = 59;
printf ("%d", a&b);
}
2, "bitwise OR" operator (|)
In two corresponding bits, as long as one is 1, the result value of this bit is 1. In the case of logic or arithmetic, it is true that 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:
Copy Code code as follows:
#include <stdio.h>
Main ()
{
int a=060;
int b = 017;
printf ("%d", a|b);
}
Application: A bitwise OR operation is commonly used to set a certain bit value of 1 for a data. For example, if you want to change the lower 4 bits of a number A to 1, you only need to bitwise or operate A and 17 (8).
3, Exchange two values, no temporary variables
For example: A=3, namely one (2); b=4, that is, 100 (2).
To swap the values of a and B, you can implement the following assignment statement:
Copy Code code as follows:
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) (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;" are equivalent to B=b∧ (A∧B).
② to execute the third assignment statement: A=a∧b. Because the value of a is equal to (A∧B), the value of B equals (b∧a∧b),
Therefore, the equivalent of A=a∧b∧b∧a∧b, that is, the value of a is equal to a∧a∧b∧b∧b, equal to B.
It's amazing!
C Language Source code:
Copy Code code as follows:
#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, "Take Back" operator (~)
He is a unary operator, which is used to find the binary inverse code of integers, that is, to change 1 of the operands on each bits to 0, and 0 to 1.
For example: ~77 (8)
Source:
Copy Code code as follows:
#include <stdio.h>
Main ()
{
int a=077;
printf ("%d", ~a);
}
5, left shift operator (<<)
The left-shift operator is used to move the bits of a number to the left several digits, the number of digits to be specified by the right operand (the right operand must be non-negative), the position vacated on the right is filled with 0, and the high left-shift overflow discards the high.
For example: Move the binary number of a to the left 2 digits, the right space out of the bit 0, the left overflow bit discarded. If a=15, that is 00001111 (2), move left 2
Bit 00111100 (2).
Source:
Copy Code code as follows:
#include <stdio.h>
Main ()
{
int a=15;
printf ("%d", a<<2);
}
The 1-bit left is equal to the number multiplied by 2, and the left 2-bit equivalent to the number multiplied by 2*2=4,15<<2=60, that is, by 4. But this conclusion only applies to the
When the number is left, the overflow discards the high position that does not contain 1.
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 overflows with 0, and the top of the overflow contains 1 when the left 2 bit is shifted.
6, right shift operator (>>)
The right shift operator is used to move the bits of a number to the right several digits, the number of digits moved by the right operand (the right operand must be non-negative), and the lower position to the right end is discarded, and for unsigned digits, a high position of 0. For a signed number, some machines will fill (i.e. "arithmetic shift") the part vacated by the left, while others fill (i.e. "logically shift") the part vacated by the left. Note
Meaning: To unsigned number, move right when the left high position into 0; for signed values, if the original symbol bit is 0 (the number is positive), the left is also moved
Into the 0. If the symbol bit turns out to be 1 (that is, a negative number), move the left to 0 or 1, depending on the computer system you are using. Some systems move into 0, and some systems move into 1. The move into 0 is called "logical shift", that is, the simple shift, and the move into 1 is called "Arithmetic shift."
Example: The value of a is octal number 113755:
A:1001011111101101 (represented in binary form)
a>>1:0100101111110110 (when logic moves right)
a>>1:1100101111110110 (when arithmetic moves right)
In some systems, A>>1 gets octal number 045766, while in 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 symbol number is shifted to the right, if the sign bit is 1, the left is 1.
Source:
Copy Code code as follows:
#include <stdio.h>
Main ()
{
int a=0113755;
printf ("%d", a>>1);
}
7, bitwise operation Assignment operator
Bitwise operators and assignment operators can compose compound assignment operators.
For example: &=, |=, >>=, <<=,∧=
Example: A & = b equals A = A & B
A << = 2 equals a = a << 2