What are bitwise operators?
I’m someone who writes code just for fun and haven’t really delved into it in either an academic or professional setting, so stuff like these bitwise operators really escapes me.
I was reading an article about JavaScript, which apparently supports bitwise operations. I keep seeing this operation mentioned in places, and I’ve tried reading about to figure out what exactly it is, but I just don’t seem to get it at all. So what are they? Clear examples would be great! ðŸ˜€
Just a few more questions – what are some practical applications of bitwise operations? When might you use them?
First answer
Since nobody has broached the subject of why these are useful:
I use bitwise operations a lot when working with flags. For example, if you want to pass a series of flags to an operation (say, File.Open()
, with Read mode and Write mode both enabled), you could pass them as a single value. This is accomplished by assigning each possible flag it’s own bit in a bitset (byte, short, int, or long). For example:
Read: 00000001
Write: 00000010
So if you want to pass read AND write, you would pass (READ  WRITE) which then combines the two into
00000011
Which then can be decrypted on the other end like:
if ((flag & Read) != 0) { //...
which checks
00000011 &
00000001
which returns
00000001
which is not 0, so the flag does specify READ.
You can use XOR to toggle various bits. I’ve used this when using a flag to specify directional inputs (Up, Down, Left, Right). For example, if a sprite is moving horizontally, and I want it to turn around:
Up: 00000001
Down: 00000010
Left: 00000100
Right: 00001000
Current: 00000100
I simply XOR the current value with (LEFT  RIGHT) which will turn LEFT off and RIGHT on, in this case.
Bit Shifting is useful in several cases.
x << y
is the same as
x * 2^{y}
if you need to quickly multiply by a power of two, but watch out for shifting a 1bit into the top bit – this makes the number negative unless it’s unsigned. It’s also useful when dealing with different sizes of data. For example, reading an integer from four bytes:
int val = (A << 24)  (B << 16)  (C << 8)  D;
Assuming that A is the mostsignificant byte and D the least. It would end up as:
A = 01000000
B = 00000101
C = 00101011
D = 11100011
val = 01000000 00000101 00101011 11100011
Colors are often stored this way (with the most significant byte either ignored or used as Alpha):
A = 255 = 11111111
R = 21 = 00010101
G = 255 = 11111111
B = 0 = 00000000
Color = 11111111 00010101 11111111 00000000
To find the values again, just shift the bits to the right until it’s at the bottom, then mask off the remaining higherorder bits:
Int Alpha = Color >> 24
Int Red = Color >> 16 & 0xFF
Int Green = Color >> 8 & 0xFF
Int Blue = Color & 0xFF
0xFF
is the same as 11111111
. So essentially, for Red, you would be doing this:
Color >> 16 = (filled in 00000000 00000000)11111111 00010101 (removed 11111111 00000000)
00000000 00000000 11111111 00010101 &
00000000 00000000 00000000 11111111 =
00000000 00000000 00000000 00010101 (The original value)
Second answer
Bitwise operators are operators that work on a bit at a time.
AND is 1 only if both of its inputs are 1.
OR is 1 if one or more of its inputs are 1.
XOR is 1 only if exactly one of its inputs are 1.
NOT is 1 only if its input are 0.
These can be best described as truth tables. Inputs possibilities are on the top and left, the resultant bit is one of the four (two in the case of NOT since it only has one input) values shown at the intersection of the two inputs.
AND0 1 OR0 1
+ +
00 0 00 1
10 1 11 1
XOR0 1 NOT0 1
+ +
00 1 1 0
11 0
One example is if you only want the lower 4 bits of an integer, you AND it with 15 (binary 1111) so:
203: 1100 1011
AND 15: 0000 1111

IS 11: 0000 1011
Third answer
These are the bitwise operators, all supported in JavaScript:

op1 & op2
— TheAND
operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0. 
op1  op2
— TheOR
operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0. 
op1 ^ op2
— TheEXCLUSIVEOR
operator compares two bits and returns 1 if either of the bits are 1 and it gives 0 if both bits are 0 or 1. 
~op1
— TheCOMPLEMENT
operator is used to invert all of the bits of the operand. 
op1 << op2
— TheSHIFT LEFT
operator moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. Each move to the left effectively multiplies op1 by 2. 
op1 >> op2
— TheSHIFT RIGHT
operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. Each move to the right effectively divides op1 in half. The leftmost sign bit is preserved. 
op1 >>> op2
— TheSHIFT RIGHT
–ZERO FILL
operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. Each move to the right effectively divides op1 in half. The leftmost sign bit is discarded.