2008-11-09 23:59:26 8 Comments

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?

### Related Questions

#### Sponsored Content

#### 26 Answered Questions

### [SOLVED] What is the scope of variables in JavaScript?

**2009-02-01 08:27:19****lYriCAlsSH****499930**View**2013**Score**26**Answer- Tags: javascript function variables scope var

#### 49 Answered Questions

### [SOLVED] Which equals operator (== vs ===) should be used in JavaScript comparisons?

**2008-12-11 14:19:49****bcasp****1764997**View**5665**Score**49**Answer- Tags: javascript operators equality equality-operator identity-operator

#### 10 Answered Questions

### [SOLVED] What is JSONP, and why was it created?

**2010-01-14 20:53:48****Cheeso****485542**View**2129**Score**10**Answer- Tags: javascript json jsonp terminology

#### 77 Answered Questions

### [SOLVED] What is the JavaScript version of sleep()?

**2009-06-04 14:41:10****fmsf****2665841**View**2335**Score**77**Answer- Tags: javascript sleep

#### 36 Answered Questions

### [SOLVED] What is the !! (not not) operator in JavaScript?

**2009-04-24 08:13:58****Hexagon Theory****551743**View**3108**Score**36**Answer- Tags: javascript operators

#### 27 Answered Questions

### [SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

**2009-08-26 16:10:13****Mark Rogers****1071369**View**7549**Score**27**Answer- Tags: javascript syntax jslint use-strict

#### 67 Answered Questions

### [SOLVED] What is the most efficient way to deep clone an object in JavaScript?

**2008-09-23 16:26:09****jschrab****2010995**View**5180**Score**67**Answer- Tags: javascript object clone

#### 21 Answered Questions

### [SOLVED] What is the difference between call and apply?

**2009-12-31 19:56:25****John Duff****702211**View**3105**Score**21**Answer- Tags: javascript performance function dynamic

#### 14 Answered Questions

### [SOLVED] What is the 'new' keyword in JavaScript?

**2009-10-29 21:32:45****Alon Gubkin****264717**View**1744**Score**14**Answer- Tags: javascript new-operator

## 9 comments

## @Prashant 2016-04-21 07:02:19

operations:bitwise AND

bitwise OR

bitwise NOT

bitwise XOR

etc

List item

Eg.

Uses of bitwise operator^{y}respectively.Eg.

Eg.

`if else`

statementEg.

Eg.

bitwise shifting works only with +ve numberAlso there is a wide range of use of bitwise logic## @Jonathan Cross 2017-09-24 13:35:31

"complixblity and reare..." ?

## @xgqfrms 2019-08-15 01:31:08

`The left-shift and right-shift operators are equivalent to multiplication and division by x * 2y respectively.`

That's right! muyiy.cn/question/program/102.html## @xgqfrms 2019-08-15 02:11:56

my solution repl.it/@xgqfrms/…

## @Ed Marty 2008-11-10 00:39:20

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:## @Ahmed C 2020-06-05 09:26:34

x<<n , so n must be in the form of 2^value?

## @Jeff Hillman 2008-11-10 00:03:13

These are the bitwise operators, all supported in JavaScript:

`op1 & op2`

-- The`AND`

operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.`op1 | op2`

-- The`OR`

operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0.`op1 ^ op2`

-- The`EXCLUSIVE-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`

-- The`SHIFT 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.## @Andrey Tyukin 2019-05-24 13:38:45

"if the bits are complementary" - waht?

## @Jeff Hillman 2019-05-29 16:05:43

@AndreyTyukin two bits are complementary if one of them is 1 and the other is 0.

## @Andrey Tyukin 2019-05-29 16:08:17

@JeffHillman According to your description in the comment, 1 and 1 are not "complementary". Then it's unclear to me why

`1 | 1`

gives`1`

and not`0`

, and how`|`

is then supposed to be different from`^`

. I had to use this Q/A as a duplicate target few days ago, and I wished that after 10 years one would have a clearer canonical duplicate for this kind of questions.## @user3677963 2014-06-20 14:30:49

It might help to think of it this way. This is how AND (&) works:

It basically says are both of these numbers ones, so if you have two numbers 5 and 3 they will be converted into binary and the computer will think

are both one: 00000001 0 is false, 1 is true

So the AND of 5 and 3 is one. The OR (|) operator does the same thing except only one of the numbers must be one to output 1, not both.

## @Nosredna 2009-02-10 00:35:44

I kept hearing about how slow JavaScript bitwise operators were. I did some tests for my latest blog post and found out they were 40% to 80% faster than the arithmetic alternative in several tests. Perhaps they used to be slow. In modern browsers, I love them.

I have one case in my code that will be faster and easier to read because of this. I'll keep my eyes open for more.

## @paxdiablo 2008-11-10 00:04:28

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.

One example is if you only want the lower 4 bits of an integer, you AND it with 15 (binary 1111) so:

## @Eugene Yokota 2008-11-10 00:26:02

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:

## @Greg Hewgill 2008-11-10 00:11:05

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.

## @click 2008-11-10 00:21:30

Wow, that makes a lot more sense now. It sounded a lot more complicated than it apparently is. Thanks. I'm not sure which to choose as the right answer as there are loads of good ones, and I can't upvote so.. thanks

## @javamonkey79 2008-11-10 00:14:43

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.

## @Si8 2016-08-03 20:50:41

`|`

is an OR operations?## @samayo 2017-03-17 10:45:34

For some reason this made the most sense to me. Still not sure about the

`x | y = 1000 0001 |`

part though