How Bitwise Operators Work

Violet Suber
Aug 23, 2017 · 5 min read

What are Bitwise Operators?

Bitwise operators are operators used in JavaScript and other programming languages that work with operands based on their binary notation as opposed to standard representation of numbers or any other type of representation. However, the inputs and outputs are still always just regular numbers; the binary conversion and how it is used is all done behind the scenes. We’re about to explore what goes on behind the scenes of bitwise operators in this article.

Binary Basics

Before getting into bitwise operators, you should have a basic understanding of how binary numbers work.

We’re typically used to thinking about numbers in base 10; how many ones, tens, hundreds, thousands, etc. make up a particular number. When using binary numbers, instead of thinking in terms of base 10, we have to use base 2. Starting from 1, instead of multiplying by 10 for each “place” to the left, we multiply by 2. Then, we use ones to designate if one instance of any of these base 2 numbers fits into the number we are trying to write, and zeroes if they do not.

For example, what if we wanted to convert the number 53 to binary?

 0     0    1    1   0   1   0   1
128 64 32 16 8 4 2 1

We can make the number 53 by adding 32, 16, 4, and 1. Therefore, the binary version of 53 would be 00110101. Each of these base 2 “places” are known as “bits.”

Signed Integers

When we work with bitwise operators, numbers get converted into “signed” integers that have 32 bits. Signed integers account for negative numbers as well as positive numbers. You can always tell if a binary number is positive or negative by looking at its leftmost bit: 1 for a negative number and 0 for a positive number. The negative version of a positive number is known as its “two’s compliment.” We can find a number’s two’s compliment by inverting the bits and adding one.

Binary notation of the number 8:
00000000000000000000000000001000
Inverted:
11111111111111111111111111110111
This is actually binary notation for -9, we need to add 1 to get -8Add 1:
11111111111111111111111111111000 = -8
Notice how the first 0 is now a 1, followed by all 0s

This also works for converting negative numbers into positives.

The Bitwise AND Operator (&)

The & operator compares each bit of two numbers and results in a 1 only if both bits are 1.

For example, let’s say we want to find the answer to 8 & 13:

(We won't use 32 bit numbers in some of these examples to make things easier to read)8:  0 0 1 0 0 0
13: 0 0 1 1 0 1
---------------
0 0 1 0 0 0 -> 8

Since only the 4th from the right bit contains a 1 in both 8 and 13, that’s the only bit that will have a 1 in the result, and therefore, 8 & 13 = 8.

The Bitwise OR Operator (|)

The | operator compares each bit of two numbers and results in a 1 if EITHER bit is 1.

Let’s compare 8 and 13 again, this time with the OR operator:

8:  0 0 1 0 0 0
13: 0 0 1 1 0 1
---------------
0 0 1 1 0 1 -> 13

In this case, every instance of 1 is included in the result, and therefore, 8 | 13 = 13.

The Bitwise XOR Operator (^)

The ^ operator compares each bit of two numbers and results in a 1 if the bits are different.

11: 0 0 1 0 1 1
7: 0 0 0 1 1 1
---------------
0 0 1 1 0 0 -> 12

In both the bits 3rd and 4th from the left, one of the operands has a 0 and the other has a 1, so the value of these bits in the result is 1. Even though the last two bits of both numbers are 1, since they are the same bits for both numbers, they result in 0. Therefore, 11 ^ 7 = 12.

The Bitwise NOT Operator (~)

The ~ operator inverts the bits of a number.

10:  00000000000000000000000000001010
~10: 11111111111111111111111111110101 -> -11

The OR operator only inverts the number, it does not add one, so a number inverted by the OR operator does not produce its two’s compliment. The result of the ~ operator used on any number is the negative version of that number plus one. Therefore, ~10 = -11.

The Left Shift Operator (<<)

The << operator shifts a number’s bits a specific amount to the left.

For example, what if we wanted to shift the number 19’s bits two bits to the left?

19:  0 0 0 1 0 0 1 1
<<2: 0 1 0 0 1 1 0 0 -> 76

Shifting each bit of 19 two places to the left, we now have the result 76.

The Right Shift Operator (>>)

The >> operator shifts a number’s bits a specific amount to the right. If any 1s have to be shifted at the very end of the number, they simply get discarded.

Here’s what would happen if we shifted the number 43’s bits three bits to the right:

43:  1 0 1 0 1 1
>>3: 0 0 0 1 0 1 -> 5

Shifting each bit of 43 three places to the right, we now have the result 5.

What would happen if we used this operator on a negative number?

-23 >> 2
-23: 11111111111111111111111111101001
>>2: 11111111111111111111111111111010 -> -6

When using >> on a negative number, it fills the leftmost bits with 1s, therefore keeping the sign bit 1 for a negative number. This means that the right shift operator is a “sign-propagating” operator.

The Zero-Fill Right Shift Operator (>>>)

The >>> operator works like the >> operator, except it is NOT sign-propagating. When bits shift over, the left bits are always filled with 0. Therefore, it is impossible to get a negative number as a result when using >>>.

Let’s say we have a negative number, negative 53, and we wanted to shift its bits one to the right with >>>.

-53:  11111111111111111111111111001011
>>>1: 01111111111111111111111111100101 -> 2147483621

Since we shifted the bits one to the right, the leftmost bit is now filled with 0. No longer with its negative sign bit, all those 1s make the result a very large number.

Practical Applications

Bitwise operators are interesting to learn about, but what purpose do they actually serve? While not commonly used in programming, they are definitely useful in some cases, such as:

  • Lower-level programming, such as working with hardware
  • Encryption
  • Working with base two numbers (for example, we can use the conditional statement ((x & (x — 1) === 0) to determine if a number x is a power of 2).
  • If you know how to apply bitwise operators to normal programming, such as with bitmasks, your code can run faster

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade