Bit Flags and Bit-Fields
C commonly uses bit flags:
#define FLAG_READ (1u << 0)
#define FLAG_WRITE (1u << 1)
#define FLAG_ERROR (1u << 2)
uint32_t flags = FLAG_READ | FLAG_WRITE;
if (flags & FLAG_ERROR) {
handle_error();
}
Rust can do the same bit operations, but one habit must change:
C: if (flags & FLAG_ERROR)
Rust: if flags & FLAG_ERROR != 0
Rust if conditions must be bool. Integers are not implicitly truthy or falsy.
Bit Flags Can Be const
C often defines masks with macros:
Read More