C language has the features of high-level language and low-level language.
The so-called bitwise operation refers to the operation of bits.
Bit operations provided by the C language:
operator meaning
& Bitwise AND
| Bitwise OR
∧ Bitwise XOR OR
∽ Reverse
<< left Shift
>> Right Shift
Description
1. The bitwise operator, except for ∽, is a two-mesh (unary) operator, which requires an operand on each side.
2, the calculation can only be shaped or character data, can not be real data.
Bitwise-AND operator (&)
The provisions are as follows:
0&0=0 0&1=0 1&0=0 1&1=1
Example: 3&5=?
First, 3 and 5 in the complement, then the bitwise AND operation.
Complement of 3:00000011
Complement of 5:00000101
&: 00000001
3&5=1
--------------------------------------------------------------------------------
Bitwise OR operator (|)
The provisions are as follows:
0|0=0 0|1=1 1|0=1 1|1=1
Example: 060|017=?
Bitwise OR operation of octal number 60 and octet 17.
060 00110000
017 00001111
|: 00111111
060|017=077
--------------------------------------------------------------------------------
The XOR operator (∧), also known as the XOR operator
The provisions are as follows:
0∧0=0 0∧1=1 1∧0=1 1∧1=0
Example: 57∧42=?
The decimal number 57 and the decimal number 42 are bitwise XOR.
57 00111001
42 00101010
∧: 00010011
57∧42=19
--------------------------------------------------------------------------------
"Inverse" operator (∽)
The provisions are as follows:
∽0=1∽1=0
Example: ∽025=?
Bitwise negation of octal number 25 (that is, binary 0000000000010101).
0000000000010101
↓
1111111111101010
∽025=177752
--------------------------------------------------------------------------------
Left shift operator (<<)
A number of the binary all shifted left several bits, if the high-left after overflow, then discard, does not work.
Example: a=a<<2
Move the binary number of a to the left by 2 bits and the right to 0.
If a=15, which is the binary number 00001111, then
A 00001111
↓↓
A<<1 00011110
↓↓
A<<2 00111100
Last a=60
--------------------------------------------------------------------------------
Right shift operator (>>)
Move the binary of a number to the right of a number of bits, and the low move out of the part discard.
Example: a=a>>2
Shift the binary number of a to the right by 2 bits and left to 0.
If a=15, which is the binary number 00001111, then
A 00001111
↓↓
A>>1 00000111
↓↓
A>>2 00000011
Last A=3
--------------------------------------------------------------------------------
Bitwise operators combine with assignment operators to make an extended assignment operator
such as: &=,|=,>>=,<<=,∧=
Example: a&=b equivalent to A=a&b
a<<=2 equivalent to A=a<<2
Bit operations with different lengths of data
If two data lengths are different (for example, long and int) for bitwise operations (such as A&b, and A is long and b is int), the system aligns the two on the right. If B is positive, the left 16 bits fill 0. If B is negative, the left should fill 1. If B is an unsigned integer type, the left end is filled with 0.
Bit Operation example
Example: Take an integer A from the right end of the 4∽7 bit
Consider the following: 1, first a right shift 4 bits, namely A>>4
2, set a low 4-bit all 0 of the number, namely ∽ (∽0<<4)
3, the above two-type and operation, namely A>>4&∽ (∽0<<4)
The procedure is as follows:
Main ()
{unsigned a,b,c,d;
scanf ("%o", &a);
b=a>>4;
C=∽ (∽0<<4);
d=b&c;
printf ("%o\n%o\n", A, b);
}
Results: 331
331 (value of a, octal)
(Value of D, octal)
Example: cyclic displacement. A right loop shift is required for a. That is, a right loop shifts n bits, moving the original left (16-n) bit in a to the right n bits. Now assume that two bytes holds an integer. As pictured on the right.
Consider the following: 1, first put a right side n bit into the high n bit in B, namely:b=a<< (16-N)
2, will a right shift n bit, its left high N-bit 0, that is c=a>>n
3, the C and B bitwise OR operation, that is c=c|b
The procedure is as follows:
Main ()
{unsigned a,b,c;int N:
scanf ("a=%o,n=%d", &a,&n);
b=a<< (16-N);
c=a>>n;
c=c|b;
printf ("%o\n%o", a,c);
}
Results: a=157653,n=3
331 (value of a, octal)
(Value of D, octal)
Bit segment
A so-called bit segment is a member of a struct type that defines the length in bits.
Example: struct packed-data
{unsigned a:2;
unsigned b:6;
unsigned c:4;
unsigned d:4;
int i;
}data;
C-Language bitwise operators