AND, OR, XOR — Bitwise Operators

ones and zeros

Let’s take a minute to breakdown bitwise operators. Bitwise operators are operators that operate on ints and uints at the binary level.

ints and uints

< 5 Seconds on Binary

0 represents closed or off or false.

1 represents open or on or true.

The binary operators take two inputs, or arguments, and returns one output. The inputs can only be 0 or 1.

AND

When we use the AND operator, we are effectively saying, if the first argument and the second argument are true, then the result is true, or 1. Otherwise the result is false, or 0.

OR

Now the OR operator is saying, if the first argument or the second argument are true, then the result is true.

XOR

Lastly, the XOR (exclusive OR) operator is saying, if either input is true, then the result is true, but if both inputs are true, then the result is false.

Another way to say it is, if one, but not both arguments are true, then the result is true.

Or, we could say, if the number of true inputs is odd, then the result will be true.

You choose.

Before we finish let’s use the XOR operator on a set of eight digits, a byte.

If you’ve ever worked with circuits, transistors, computers, code or electricity, you might see how useful these three operators can be.

Software developer. Turing School alum.

Software developer. Turing School alum.