Because the exercise demonstrates breaking a value into bits using a bit field and a union.
Assuming you know what a join is, if you are extracting something less repetitive from a binary value, then you can use it for clarity instead of saying two 24-bit 48-character integers from shifts and masks.
But for an example in a task, shifts and masks would be cleaner code, so you probably would not use union for this task.
void disp_bits(unsigned b) { // not tested for ( int shift = 7; shift >= 0; --shift ) cout << ( ( b >> shift ) & 1 ) << ' '; cout << "\n"; }
source share