Before learning about data types, let’s first understand the number systems.
1. Decimal (十进制)
The number system we use daily, with a base of 10, using digits 0-9.
123 = 1×10² + 2×10¹ + 3×10⁰
2. Binary (二进制)
With a base of 2, using digits 0 and 1.
1101₂ = 1×2³ + 1×2² + 0×2¹ + 1×2⁰
= 8 + 4 + 0 + 1
= 13₁₀
3. Hexadecimal (十六进制)
With a base of 16, digits: 0-9, A-F (A=10, B=11, C=12, D=13, E=14, F=15)
A3F₁₆ = A×16² + 3×16¹ + F×16⁰
= 10×256 + 3×16 + 15×1
= 2560 + 48 + 15
= 2623₁₀
4. Binary Conversion Table
Every 4 bits of binary corresponds to 1 hexadecimal digit.
| Decimal | Binary | Hexadecimal |
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
5. Byte (字节)
1 byte = 8 bits
Binary representation of a byte
// Example of one byte: 170 (decimal)
Byte value: 170
Binary: 10101010
Hexadecimal: AA
Bit positions: 7 6 5 4 3 2 1 0
Bit values: 1 0 1 0 1 0 1 0
Weights: 2⁷ 2⁶ 2⁵ 2⁴ 2³ 2² 2¹ 2⁰
Calculation: 128 + 0 + 32 + 0 + 8 + 0 + 2 + 0 = 170
Value range:
-
Unsigned: 0 to 255
The maximum value in binary is 1111 1111, which has 8 bits, the leftmost bit represents the highest bit, and the rightmost bit represents the lowest bit.
Unsigned means that the highest bit is not a sign bit, representing a specific number, so the highest bit participates in counting. For example, the data type unsigned char is an unsigned data type.
Signed means that the highest bit is the sign bit, 0 represents positive, 1 represents negative, so the highest bit does not participate in counting. For example, the data type char is a signed data type.
-
Signed: -128 to 127
The maximum value in binary is 0111 1111
The minimum value in binary is 1000 0000, negative numbers are represented in two’s complement
6. Printing Numbers
#include <stdio.h>
int main(int argc, char *argv[]) { int decimal = 100; // Hexadecimal, starts with 0x int hex = 0x64;
printf("Decimal: %d\n", decimal); // %x indicates output in hexadecimal format, %d indicates output in decimal format printf("Hexadecimal: %x, %d\n", hex, hex);
return 0;}
Binary: The foundation of computers, each bit can only be 0 or 1
Hexadecimal: A compact representation of binary, easier for humans to read
Byte: The basic unit of computer storage, consisting of 8 bits