Bit Fields in C
Level 4249 keystrokes
Uses bit fields to pack multiple values into a single structure efficiently.
C code
// Struct with bit fields for compact storage1struct Flags { // 1 bit for active flag (0 or 1)2 unsigned int active : 1; // 1 bit for ready flag3 unsigned int ready : 1; // 1 bit for error flag4 unsigned int error : 1; // 4 bits for mode (0-15)5 unsigned int mode : 4;6};7 // Values in member order: active=1, ready=0, error=0, mode=58struct Flags f = {1, 0, 0, 5};// Bit fields are read like normal members: 1 and 59printf("Active: %d\n", f.active);10printf("Mode: %d\n", f.mode);// 7 bits fit in one unsigned int: 4 bytes on common compilers11printf("Size: %lu bytes\n", sizeof(f));Output
Active: 1 Mode: 5 Size: 4 bytes
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C code and how often they appear.
;× 9:× 7,× 6"× 6(× 4)× 4\× 3%× 3.× 2{× 2