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! :D
Just a few more questions - what are some practical applications of bitwise operations? When might you use them?
operations:
bitwise AND
bitwise OR
bitwise NOT
bitwise XOR
etc
List item
Eg.
Uses of bitwise operator
Eg.
Eg.
if else
statmentEg.
Eg.
bitwise shifting works only with +ve number
Also there is a wide range of use of bitwise logic
To break it down a bit more, it has a lot to do with the binary representation of the value in question.
Hope this helps.
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
-- TheEXCLUSIVE-OR
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
-- The COMPLEMENT
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
--The SHIFT 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 left-most sign bit is preserved.op1 >>> op2
--The SHIFT 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 left-most sign bit is discarded.It is worth noting that the single-bit truth tables listed as other answers work on only one or two input bits at a time. What happens when you use integers, such as:
The answer lies in the binary expansion of each input:
Each pair of bits in each column is run through the "AND" function to give the corresponding output bit on the bottom line. So the answer to the above expression is 4. The CPU has done (in this example) 8 separate "AND" operations in parallel, one for each column.
I mention this because I still remember having this "AHA!" moment when I learned about this many years ago.
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:
So if you want to pass read AND write, you would pass (READ | WRITE) which then combines the two into
Which then can be decrypted on the other end like:
which checks
which returns
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:
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.
is the same as
if you need to quickly multiply by a power of two, but watch out for shifting a 1-bit 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:
Assuming that A is the most-significant byte and D the least. It would end up as:
Colors are often stored this way (with the most significant byte either ignored or used as Alpha):
To find the values again, just shift the bits to the right until it's at the bottom, then mask off the remaining higher-order bits:
0xFF is the same as 11111111. So essentially, for Red, you would be doing this:
When the term "bitwise" is mentioned, it is sometimes clarifying that is is not a "logical" operator.
For example in JavaScript, bitwise operators treat their operands as a sequence of 32 bits (zeros and ones); meanwhile, logical operators are typically used with Boolean (logical) values but can work with non-Boolean types.
Take expr1 && expr2 for example.
As others have noted, 2 & 4 is a bitwise AND, so it will return 0.
You can copy the following to test.html or something and test: