AND, OR, XOR — Bitwise Operators

Kris Sparks
2 min readFeb 5, 2018
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.

0 AND 0 = 0
1 AND 0 = 0
0 AND 1 = 0
1 AND 1 = 1
AND is represented by the ampersand - 1 & 1 = 1

OR

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

0 OR 0 = 0
1 OR 0 = 1
0 OR 1 = 1
1 OR 1 = 1
OR is represented by the vertical bar (pipe) - 1 | 1 = 1

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.

0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 1 = 1
1 XOR 1 = 0
XOR is represented by the upwards caret - 1 ^ 1 = 1

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

    01011000
XOR 10111001
=> 11100001

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

--

--