& Bitwise and
Bitwise OR
Bitwise XOR
Take the opposite attitude
& lt& lt move left.
& gt& gt description of right shift: 1. All bit operators except ∽ are binary (meta) operators, that is, one operand is required for each side. 2. Operands can only be plastic or character data, not real data. Bitwise AND operator (&; ) Provisions are as follows: 0&; 0 = 0 0 & amp 1=0。 0 = 0 1 & amp; 1= 1 Example: 3&; 5=?
Firstly, 3 and 5 are represented by complement, and then bitwise AND operation is performed. 3' s complement: 000000 1 1
Complement of 5: 00000 10 1-.
& amp:0000000 13 & amp; 5= 1 The bitwise OR operator (|) is specified as follows: 0 | 0 = 00&; 1= 1。 0 = 1 1 & amp; 1= 1 For example: 060|0 17=?
Bitwise OR operation of octal number 60 and octal number 17. 060 00 1 10000
0 17 0000 1 1 1 1 -
|: 001111160 | 017 = 077 XOR operator (∧), also known as XOR operator, is specified as follows: 0 ∧ 0 =
Decimal number 57 and decimal number 42 are bitwise XOR. 57 00 1 1 100 1
42 00 10 10 10 -
∧: 000100157 ∧ 42 =19 The negation operator (∽) is specified as follows: ∽ 0 =1∽ 65;.
Bitwise negation of the octal number 25 (that is, binary 000000000101). 00000000000 10 10 1
↓
1111111111. If the high bit moves to the left and overflows, it will be discarded and will not work. For example: a = a
Shift the binary number of a by 2 digits to the left and add 0 to the right.
If a= 15, that is, the binary number 000011,then a000011.
↓ ↓
a & lt& lt 1 000 1 1 1 10
↓ ↓
A< & lt20011100 The last a=60 right shift operator (>>) shifts all the binary digits of a number to the right, discarding the low bits. Example: a = a>& gt2
Shift the binary number of A by 2 digits to the right and add 0 to the left.
If a= 15, that is, the binary number 000011,then a000011.
↓ ↓
a & gt& gt 1 00000 1 1 1
↓ ↓
A> & gt2000000011Finally, the a=3-bit operator can be combined with the assignment operator to form an extended assignment operator, such as: =, | =,>& gt=,<& lt=, ∧ = Example: A&=b is equivalent. B A < < = 2 is equivalent to a = a
Example: Take an integer A with 4∽7 digits from the right and consider as follows: 1. First of all, A moves four digits to the right, that is, a>& gt4 2. Set a number with all the lower four digits zero, that is, ∽ (∽ 0 < <; 4) 3. and the above two expressions, that is, A >;; & gt4 & amp∽(∽0 & lt; & lt4) The program is as follows: main() {unsigned a, b, c, d; & lt/p & gt; & ltp & gtscanf("%o ",& ampa); & lt/p & gt; & ltp & gtb = a & gt& gt4; & lt/p & gt; & ltp & gtc = ∽(∽0 & lt; & lt4); & lt/p & gt; & ltp & gtd = b & ampc; & lt/p & gt; & ltp & gtprintf("%o\n%o\n ",a,b); & lt/p & gt; & ltp> Result: 331↙ 331(value of a, octal)15 (value of d, octal) Example: cyclic shift. It is necessary to shift a in the correct cycle. That is to say, the A cycle is shifted by n bits to the right, and the original left (16-n) bits in A are shifted by n bits. Now suppose that two bytes hold an integer. As shown on the right. Consider the following: 1. First, put the right n bit of A into the high n bit of B, that is, B = A;; & gtN 3。 Perform bitwise OR operation on C and B, that is, C = c=c|b B B. The program is as follows: main() {unsigned a, b, c; int n:& lt; /p & gt; & ltp & gtscanf("a=%o, n=%d ",& i, & n); & lt/p & gt; & ltp & gtb = a & lt& lt( 16-n); & lt/p & gt; & ltp & gtc = a & gt& gtn; & lt/p & gt; & ltp & gtc = c | b; & lt/p & gt; & ltp & gtprintf("%o\n%o ",a,c); & lt/p & gt; & ltp> results: A = 157653, n = 3 ↙ 331(value of a, octal)15 (value of d, octal).
A bit segment is a member of a structural type, and its length is defined by bits. Example: structured packaged data (unsigned a: 2; & lt/p & gt; & ltp> unsigned b: 6; & lt/p & gt; & ltp> unsigned c: 4; & lt/p & gt; & ltp> unsigned d: 4; & lt/p & gt; & ltp & gtint I; & lt/p & gt; & ltp>} data;