#first lecture
#bit operations
# & operation (and)
& | 1 | 0 |
---|---|---|
1 | 1 | 0 |
0 | 0 | 0 |
#|Operation (or)
| | 1 | 0 |
---|---|---|
1 | 1 | 1 |
0 | 1 | 0 |
# ^Operation
^ | 1 | 0 |
---|---|---|
1 | 0 | 1 |
0 | 1 | 0 |
# ~(Negative)
~ | Negate the result |
---|---|
1 | 0 |
0 | 1 |
#signed and unsigned
The first bit of the signed number is "binary", which represents the sign, 1 is negative and 0 is positive.
The first "binary" of an unsigned number does not represent a symbol, but only a value.
Unsigned & Signed Numeric Value
x | B2U(X) | B2T (X) |
---|---|---|
0000 | 0 | 0 |
0001 | 1 | 1 |
0010 | 2 | 2 |
0011 | 3 | 3 |
0100 | 4 | 4 |
0101 | 5 | 5 |
0110 | 6 | 6 |
0111 | 7 | 7 |
1000 | 8 | -8 |
1001 | 9 | -7 |
1010 | 10 | -6 |
1011 | 11 | -5 |
1100 | 12 | -4 |
1101 | 13 | -3 |
1110 | 14 | -2 |
1111 | 15 | -1 |
TMax && TMin && UMax
TMax: The maximum value of a signed number, that is, the case where the sign bit is 0 in binary and the other bits are 1.
TMin: The minimum value of a signed number, that is, the case where the sign bit is 1 in binary and the other bits are 0.
UMax: The maximum value of an unsigned number, that is, the case where all bits in binary are 1
Law |TMin| = TMax + 1;
|UMax| = 2 * TMax + 1;
8 | 16 | |
---|---|---|
UMax | 255 | 65,535 |
TMax | 127 | 32,767 |
TMin | -128 | -32,768 |
sample form
Constant1 | Constant2 | Relation | Evaluation |
---|---|---|---|
0 | 0U | == | unsigned |
-1 | 0 | < | signed |
-1 | 0U | > | unsigned |
2147483647 | -2147483647-1 | > | signed |
2147483647U | -2147483647-1 | < | unsigned |
-1 | -2 | > | signed |
(unsigned)-1 | -2 | > | unsigned |
2147483647U | 2147483648U | < | unsigned |
bit extension and contraction
1. Extension:
(1) If it is a signed number, extend the leftmost sign bit without changing the value of the number.
(2) If it is an unsigned number, extend the leftmost number of digits, if it is 1, it will double and add 1; if it is 0, it will not change.
2. Shrink:
(1) If the leftmost and the leftmost one are the same and signed, we do not change the value.
(2) If it is signed, and the leftmost and the leftmost are not the same, the leftmost bit is 1, it becomes larger, and the leftmost bit is 0, it decreases.
(3) If it is unsigned, the leftmost bit is 0 and does not change, and the leftmost bit is 1 and changes.