Tsallaka zuwa abun ciki
Justice Tech School

Bitwise Operators

& | ^ ~ << >> work on individual bits. They are used for flags, masks and low level protocols.

Example
unsigned flags = 0;
flags |= 1u << 2;
flags |= 1u << 0;
printf("%u %d\n", flags, (flags & 4) != 0);
flags &= ~1u;
printf("%u %u %u\n", flags, 6u & 3u, 6u ^ 3u);

Result

5 1
4 2 5

Exercise

Test bit 3.

if (flags  (1u << 3)) {}

Quiz Test

Which clears a bit?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.