1

I was just experimenting and tried putting this in the console:

4 | 2 | 4 | 1 | 10

returns 15 in console..

4 | 2 | 4 | 3 | 1

returns 7 in console..

I tried that on Chrome and Firefox.

Why?

I'm just starting with learning JavaScript... maybe I'm missing a concept here?

3
  • 3
    What were you expecting it to return, and why? Commented Feb 22, 2012 at 3:05
  • What do you expect it to return? Commented Feb 22, 2012 at 3:05
  • Well I've never worked with bitwise operators as the other folks mentioned below.. personally, I was expecting 'true', but this is JavaScript so I'm not used to its concepts and quirks yet. Commented Feb 22, 2012 at 3:08

3 Answers 3

6

The | operator in JavaScript is a bitwise integer OR operator. So it's doing an OR operation on the bits you're giving it, resulting in 15.

A bitwise OR operation takes each bit in the value and sets the corresponding bit in the result if either of the input bits in that position is set. So

4  is 0100 in binary
2  is 0010
4  is 0100
1  is 0001
10 is 1010
      ----
      1111 = 15 decimal

Update: In a comment on your question, you've said you were expecting true rather than 15. If so, you want the logical OR operator, ||, not the bitwise operator, although || may also surprise you with what it returns (4 || 2 || 4 || 1 || 10 = 4, not true), as JavaScript's logical OR (||) is curiously powerful, more so than in many other languages.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes... in binary it makes lots of sense: 0100 | 0010 | 0100 | 0001 | 1010 = 1111 = 15
@JanCarloViray: Probably millions of them. Bitwise operations are very popular in computing. Probably not as popular in the typical environment where JavaScript is used (web browsers), but I'm sure there are some. And certainly when JavaScript is used in other environments, such as NodeJS where you do operations like open files and use the | to combine the various file mode flags.
Not really in javascript, but, in embedded programming. YES. In fact, bit masking (using OR and AND effectively) is one of the first things taught in any embedded programming book/class.
3

You're using an OR operation

If you want a true/false or, you'll want ||.

4 Comments

Note, using the logical || operator is not going to result in either true or false with those integer operands. Both cases will simply result in 4
I know, but, if he wants true/false.
@DhaivatPandya: Again, Phil's point is that || won't give you true or false either.
Ah, now that I read it again makes sense. What I meant to say was if you wanted to find out whether or not somethings are true when ORed in the English sense, you do not want to use the bit operator.
2

The | operator is the bitwise or operator.

The | operator lines up the binary digits of each operand, and returns 1 for that place if there is a 1 in that place either or both of the operands.

For example, let's look at what 3 | 10 does:

3 is 11 in binary. 10 is 1010 in binary.

Line them up, and you get

3      - 0011
10     - 1010
Result - 1011

The result 1011 is 11 in decimal, so the result of this example is 11.

Here's one of the examples in your question 4 | 2 | 4 | 1 | 10

 4 - 0100
 2 - 0010
 4 - 0100
 1 - 0001
10 - 1010
 | ======
     1111

And 1111 is binary for 15, which was the result you got.

The bitwise or operator, along with other bit manipulation operators are generally used for low-level computations. For example, you can implement arithmetic like multiplication, addition, and division entirely with bitwise operators.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.