You can set a bit by ORing (|
) with a value that has that bit set.
You can unset a bit by ANDing (&
) with a value that has every bit set except that one.
You can turn a value that has one bit set into a value that has every bit except that one set with the NOT (~
) operator.
You can tell if a bit is set by ANDing (&
) with a value that only has that bit set, and seeing whether the result is true or false.
You can make a value with the nth bit (counting from the right, with the rightmost bit named the 0th bit) set by left-shifting (<<
) the value 1
by n places.