More on Using Bitwise Operations
Learn how we can use bitwise operations to calculate bitmasks and do optimizations for arithmetic calculations.
We'll cover the following...
Computing bitmasks
Here is another example of using bitwise operations. We need them for computing bitmasks, which use OR or AND operators to encode or decode values respectively. We’re already familiar with file permission masks in the Unix environment. Let’s suppose that a file has the permissions “-rw-r–r–”. It looks like this in binary:
0000 0110 0100 0100
Let’s suppose that we want to check if the file owner can execute it. We can do that by calculating the bitwise AND for the permission mask and the 0000 0001 0000 0000 0000
number. Here is the calculation:
0000 0110 0100 0100 & 0000 0001 0000 0000 = 0000 0000 0000 0000 = 0
The result equals zero. This means that the owner cannot execute the file.
Using the bitwise OR, we can set bits of the bitmask. For example, we can allow the owner to execute the file like ...