Integer Representation p42
| Decimal |
Binary |
Octal |
Hex |
| 0 |
0000 |
0 |
0 |
| 1 |
0001 |
1 |
1 |
| 2 |
0010 |
2 |
2 |
| 3 |
0011 |
3 |
3 |
| 4 |
0100 |
4 |
4 |
| 5 |
0101 |
5 |
5 |
| 6 |
0110 |
6 |
6 |
| 7 |
0111 |
7 |
7 |
| 8 |
1000 |
10 |
8 |
| 9 |
1001 |
11 |
9 |
| 10 |
1010 |
12 |
A |
| 11 |
1011 |
13 |
B |
| 12 |
1100 |
14 |
C |
| 13 |
1101 |
15 |
D |
| 14 |
1110 |
16 |
E |
| 15 |
1111 |
17 |
F |
| 16 |
10000 |
20 |
10 |
| 17 |
10001 |
21 |
11 |
| 18 |
10010 |
22 |
12 |
Values & Ranges



- Use
<limits.h> for platform-specific min/max values (e.g., INT_MAX, UINT_MAX)
Shift Operations
- Left shift (<<): Shift bits left, fill with 0's on right
- Right shift (>>):
- Logical: Shift bits right, fill with 0's on left
- Arithmetic: Shift bits right, replicate most significant bit on left
Two’s complement representation


Conversion
- For positive numbers (+)
- complement ← original (补码=原码)
- original ← complement (原码=补码)
- For negative numbers (-)
- complement = reversed(abs(original)) + 1 (补码=正数的反码+1)
- original ← -reverse(complement-1) (原码= 补码-1,反转,再加负号)
- All binary (addition/subtraction) calculations are done directly with complements (in a computer)
Casting
Explicit Casting
(int) for signed
(unsigned) for unsigned
Implicit Casting