Bitwise inverse (~) Unlike (&) or (^) or (|) shift right (>) shift left (<), Inverse Operation
We first know the characteristics of these two binary data:1 = 0000 0000 0000 0000 0000 0000 0000 0001
-1 = 1000 0000 0000 0000 0000 0000 0000 0001
1. The highest bit (first) indicates positive and negative (0 is positive, 1 is negative)
2. parity (last) indicates parity (0 is an even, and 1 is an odd)
I. bitwise inversion (~)
Decimal 1 is reversed by bit =?
Analysis:
1. Convert decimal 1 to binary: 1 = 0000 0000 0000 0000 0000 0000 0000 0001
2.Bitwise Inversion: Obtain a new binary value from the original binary value. If the original value is 0, the value of is changed to 0.
1 get 1111 1111 1111 1111 1111 1111 1111 1110 after bitwise Inversion
3. The highest bit (first) of binary indicates positive and negative (1 is negative, 0 is positive). If it is negative, if it is negative, you must useComplement.
Complement: Complement = after the sign bit (highest bit), the bitwise result is reversed and 1 is added.
1000 0000 0000 0000 0000 0000 0000 0010
4. The result is: Convert the complement code to decimal:-2;
~ 1 =-2
Decimal-1 is reversed by bit =?
1. the binary value of-1 is 1000 0000 0000 0000 0000 0000 0000 0001
2. The negative number is based on itsComplementStored in the form
-1 memory in the computer is 1111 1111 1111 1111 1111 1111 1111 1111
3.Bitwise inversion:0000 0000 0000 0000 0000 0000 0000
4. The result is: Convert to decimal: 0.
~ -1 = 0
2. bitwise and (&)
Bitwise and (&): If two digits with the same digit are 1, the value is 1. If one digit is not 1, the value is 0.
25 & 3 => 25 = 0000 0000 0000 0000 0000 0000 0001
3 = 0000 0000 0000 0000 0000 0000 0000 0011
Bytes -------------------------------------------------------------------------------------
0000 0000 0000 0000 0000 0000 0000
25 & 3 = 1
3. bitwise OR (^)
By bit or (^ ):If the same bit is different, it is 1. If the same bit is different, it is 0.
25 ^ 3 => 25 = 0000 0000 0000 0000 0000 0000 0001
3 = 0000 0000 0000 0000 0000 0000 0000 0011
Bytes -------------------------------------------------------------------------------------
0000 0000 0000 0000 0000 0000 0001
25 & 3 = 26
4. bitwise OR (|)
Bitwise OR (| ):If one bit is 1, it is 1.
25 | 3 => 25 = 0000 0000 0000 0000 0000 0000 0001
3 = 0000 0000 0000 0000 0000 0000 0000 0011
Bytes -------------------------------------------------------------------------------------
0000 0000 0000 0000 0000 0000 0001
25 | 3 = 27
5. Right Shift (>)
Shift the position of 1 to n places to the right. If the value is exceeded, remove it.
15 = 0000 0000 0000 0000 0000 0000 0000
15> 1 = 0000 0000 0000 0000 0000 0000 0000
15> 1 = 7
: Shifts one digit to the right by 21, and returns an integer.
N> 4 = n/(24)
64> 4 = 4
6. Shift left (<)
Shift the position of 1 to the left n places. If it is exceeded, it will be discarded.
: Shift 1 to the left, that is, multiply by 21;, and take the integer.
N <4 = n * 24
2 <4 = 32