1. Computing
Send to a Friend via Email

Bitwise Operators

Join the Discussion

Questions? Comments?

There is one group of Javascript operators that I omitted covering in the Learn Javascript tutorials. No I didn't forget them, I deliberately left them out because they don't actually get used very often and there were enough things that beginners need to learn in order to use javascript without telling them about things that most of them will never need to use.

The group of operators that I left out are the bitwise operators. In order to understand what they do you need to have an understanding of how computers store information so we'll start there. If you already know about bits and bytes feel free to skip ajead.

Computers don't actually understand characters or even numbers. All they understand is on and off. A given location in memory can either be turned on or turned off. To allow us to get computers to work with numbers we treat these on and off signals as binary numbers and refer to them as 1 when it is on and 0 when it is off. We can then translate these binary numbers into the decimal numbers that we are more familiar with and can thereforwe get the computer to easily handle numbers even though it doesn't know what numbers are.

To get the computer to handle characters other than numbers we need to allocate a specific number to represent each of those characters. There are several different ways that computers map the characters. Popular character encoding methods are EBCDIC, ASCII, and unicode. The first two of these allow for up to 256 different characters and the number of binary digits (usually abbreviated to bits) needed to store 256 different values is 8. Most computers therefore work with bytes of memory each of which consists of eight bits. Unicode uses between one and four bytes to represent just about any possible character in any possible language.

The bitwise operators as their name suggests actually work on individual bits within the computer rather than working with bytes (or characters) the way that all the other Javascript operators do. Since most people don't normally need to reference individual bits the bitwise operators don't get used very much. There are a number of quite powerful manipulations that can be done much faster and easier using bitwise operations than can be done in any other way. Some examples of useful tasks that can be done with bitwise operators include:

  • setting up multiple flags in a much smaller space - bit operators work with 31 bit signed numbers and so 32 flags can be stored in those 4 bytes instead of requiring one byte each
  • manipulating hexadecimal fields such as IP addresses and colour codes
  • exchanging the values in two fields without requiring any temporary fields

So what bitwise operators are there and what do they do?

  • & is the bitwise AND operator. There are four possible outcomes from a comparison involving this operator as illustrated in the and table below. By setting up the second value as a mask you can zero out the specific bits in the first value that you have set to zero in the second one.
  • | is the bitwise OR operator. Again, there are four possible outcomes from a comparison involving this operator as illustrated in the or table below. By setting up the second value as a mask you can convert the specific bits in the first value that you have set to one in the second to also be one.
  • ^ is the bitwise exclusive OR operator. Again four possible outcomes as illustrated in the xor table below. By setting up the second value as a mask you can invert selected bits in the first.
and
&01
000
101
or
|01
001
111
xor
^01
001
110
  • ~ is the bitwise ones complement operator. It takes just one value and will invert each bit within that value. All of the 0 will become 1 and the 1 will become 0. A fast way of changing the sign of a number would be to use this operator on the field and then add one to the result.
ones complement example
~79300000000000000000000001100011001
-79211111111111111111111110011100110
  • << is the bitwise left shift operator and moves all of the bits one to the left. The leftmost of the 32 bits is dropped and a 0 is added to the end.
left shift example
129<<300000000000000000000000010000001
103200000000000000000000010000001000
  • >> is the bitwise sign propogating right shift operator. All bits are moved one to the right with the rightmost being dropped. The new bit added to the left of the value will have the same value as that immediately to its right.
sign propogating right shift example
-16>>311111111111111111111111111101111
-211111111111111111111111111111101
  • >>> is the bitwise zero fill right shift operator. All bits are moved one to the right with the right most being dropped. A zero bit is added to the left end.
zero fill right shift example
-16>>>311111111111111111111111111101111
53687091000011111111111111111111111111101

These operators can also be combined with = to make a bitwise assignment operator. This is particularly useful sometimes for those operators that require two fields rather than just one (& | ^) where the result is to be loaded into one of those two fields. For example A |= B is equivalent to A = A | B.

Oh, and if you are wondering which of these operators you would use to exchange the contents of two fields without the use of a temporary field the answer is to se three exclusive ors. To swap A and B you would use:

A ^= B;
B ^= A;
A ^= B;

©2014 About.com. All rights reserved.