Step 4 of 7
Fixed-width types
<stdint.h> gives types with an exact size on every platform:
| Type | Size | Use it for |
|---|---|---|
int8_t / uint8_t |
1 byte | raw bytes, pixels |
int32_t / uint32_t |
4 bytes | file formats, protocols |
int64_t / uint64_t |
8 bytes | big counters, hashes, timestamps |
Whenever data leaves the program (files, network packets, hashes), professional code uses these instead of int or long, so it means the same thing on every machine.
To print them portably, <inttypes.h> has format macros: printf("%" PRIu64 "\n", x);
Your turn: implement the 64-bit FNV-1a hash, a simple, widely used string hash:
hash = 14695981039346656037
for each byte b of the string:
hash = hash XOR b
hash = hash * 1099511628211 (wrapping, which uint64_t does for free)
Previous: Signed overflow is undefined Next: The signed/unsigned comparison trap