Yes, the correct way to manipulate bits is:
-
To turn a bit on, use or mask.
-
To turn a bit off, use and (not mask).
-
To determine if a bit is on or off, use and mask (in an if expression) and test for zero.
-
You cannot both set/reset and test a bit in a single operation (in a high-level language such as Perl). Remember that race conditions can occur in multi-threaded applications, because the operation is not “atomic.”
You do not have to provide leading zeros.