Realistic task for teaching bit operations I'm looking for a function or algorithm that requires extensive bit manipulation, but is not complicated in its nature and purpose, so that students remain focused on bit operations.
Now I use Win32 API GetVersion(): it returns system version components encoded in a 32-bit integer. Although deprecated, it's a comprehensive real-world example that requires masking, shifts, and bit testing. Students also learn to figure out needed operations from documentation.
However, I'd like to move away from MS-Windows. What an equivalent facility might be? It may even be an external library, if it's free and easy to plug in (we already use libcurl).
The language is C++. Students are 1st year, not pure CS (industrial automation).
Maybe a bit too complicated, but fun nevertheless: Teach them about bitboards.
Conveniently enough, a chess-board has precisely 64 squares, so a 64-bit integer can store one bit of boolean information per square. Now the whole state of the board (disregarding en-passant and castling for now) can be encoded by having one integer per type of figure and one integer for all white figures.

Want to find the white pawns? use pawn &amp; white.

The black pawns? pawn &amp; !white

Want to find all occupied squares? all = pawn | rook | knight | bishop | king | queen

Squares reachable by white pawns in a normal move? (pawn &amp; white) &lt;&lt; 8 &amp; !all

In a two square move? (((pawn &amp; white &amp; 0xff00) &lt;&lt; 8 ) &amp; (!all) ) &lt;&lt; 8) &amp; !all


The possibilities range from trivial to requiring detailed understanding of bit-operations and they are indeed used by real chess engines. If you provide a bit of code to visualize the results, these might also be fun to simply toy around with.