Section 3: Bitwise Operations

So far in this chapter we looked at how we can use binary to achieve boolean operations. However, in this section we are going to go the other way around and use boolean operations on binary numbers. These are called bitwise operations because we apply each operation to individual bits of larger binary numbers. (A bit is a single binary digit…a 1 or a 0.) This is a bit more restrictive and specific than the example of just ignore the leading digit as we saw in the previous section. Instead we’re going to just treat each bit as its own number, but keep them all together for the final result.

Let’s just in with an example of bitwise OR.

0001
OR 1011
= 1011

What I did here was look at each digit place separately and apply the operation. For the farthest right digit, we have 1 OR 1, which gives a 1 in the result. Next we have a 0 OR 1 resulting in a 1. Then 0 OR 0 giving a 0. And finally in the left-most place we have 0 OR 1 giving a 1.

The question is why would we ever want to do this? Well it just so happens that bitwise operations have an incredibly important use in file permissions. Standard file permissions often include a 9-digit binary number to represent the read (r), write (w), execute (x) abilities of the user, group, and public.

User Group Public
r w x r w x r w x
1 1 1 1 0 1 1 0 0

In this example, the user who owns a file is able to read, write, and execute the file. Any other users in the owner’s group can read and execute the file (but not write to it), and any other user can read the file, but not write to it or execute it.

The bitwise operations assist with this because if I want to combine two permission sets, for example, I can bitwise-OR the two numbers together, then any bit that was a 1 in either set will be a 1 in the output.


Similarly, we have a bitwise-AND that does the same thing except with the AND operation.

0110
AND 1100
= 0100

This is called bit masking because an AND operation essentially applies a filter to mask any bits that are 0’s in the mask. In this example I was looking for 8’s and 4’s digits (because the 8 and 4 digits are “on” in the mask) and I found a 4.

Bitwise-AND is useful to check if something has certain permissions. In the permissions example above, if I want to know if the public has the ability to read the file I can use a bit mask of 000000100.

111101100
AND 000000100
= 000000100

Since the output is nonzero, then those permissions are present. However, if I wanted to check if the owner’s group has permission to write to the file:

111101100
AND 000010000
= 000000000

The result is zero, so no they do not have such permissions. This process is not limited to a single bit “on” at a time. I could quite easily have any number of bits “on” and see whichever permissions I care about.

Lastly, for these bitwise operations we actually have special operators we use in Java that look a bit familiar.

Operation Symbol Description
Bitwise OR | A single pipe (the OR symbol from Java…but only one of them!).
Bitwise AND & A single ampersand (the AND symbol from Java…but only one of them!).
Bitwise XOR ^ A caret (exponentiation symbol).
Bitwise NOT ~ A tilde (the squiggly symbol in the upper left of the keyboard).

We can write these in Java, or many other languages, to see them in action. For example, it is valid code to write:

int x = 6 | 10;

This will give x the value 14.

0110
OR 1010
=1110

Try some bitwise operations in a Java program to see them in action.

This takes us to the end of another chapter! Now you have added some knowledge about binary numbers and their connection to boolean operations. In the next chapter we will look at how these concepts have been applied to computers at a low level, bot physically (hardware) and programmatically (software).


← Previous PageNext Page →