6

I have a JS array which is being used as follows in our existing code:

temp = charArray[0 | Math.random() * 26];

Wanted to know what exactly is the usage of "|" symbol in the above code and are there more such operators?

Constantinius
  • 34,183
  • 8
  • 77
  • 85
shiv2685
  • 73
  • 3
  • Google's awesome. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_OR – Adam Jenkins May 20 '15 at 13:13
  • possible duplicate of [What does the "|" (single pipe) do in JavaScript?](http://stackoverflow.com/questions/6194950/what-does-the-single-pipe-do-in-javascript) – Girish May 20 '15 at 13:14
  • http://stackoverflow.com/questions/7487977/using-bitwise-or-0-to-floor-a-number – TZHX May 20 '15 at 13:14
  • @Girish The problem with the Q you link to is that the answers don't apply here. The accepted one is even false. – Denys Séguret May 20 '15 at 13:16

2 Answers2

10

From the MDN:

Bitwise operators treat their operands as a set of 32 bits (zeros and ones) and return standard JavaScript numerical values.

As the 32 bit part is (a part of) the integer part of the IEEE754 representation of the number, this is just a trick to remove the non integer part of the number (be careful that it also breaks big integers not fitting in 32 bits!).

It's equivalent to

temp = charArray[Math.floor(Math.random() * 26)];
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
3

| is bitwise OR, which means, that all bits that are 1 in either of the arguments will be 1 in the result. A bitwise OR with 0 returns the given input interpreted as an integer.

In your code the its majorily used to convert the Math.random() number to integer. The bottom line is :

var a = 5.6 | 0 //a=5
Explanation: Lets take

var a = 5; //binary - 101
var b = 6; //binary - 110

  a|b                a|a            a|0
  101                101            101
  110                101            000
 ------             ------         ------
  111-->7            101-->5        101-->5
Amit
  • 427
  • 4
  • 16
  • Bitwise OR is not the same as an addition! It only so happens that 0 | x = x, same as 0 + x = x. x | x on the other hand is not 2*x but x! – Dakkaron May 20 '15 at 13:37
  • @Dakkaron: I think I didn't make myself clear there. I meant its "BITWISE Addition". As for your example, x|x =x, say x = 5 ie 101 lets add 101 | 101 --> 101 = 5. As I said, you might wanna have to o through binary additions to understand that little pipe. – Amit May 20 '15 at 16:03
  • Well, I do understand bitwise operations, that's why I tried to clarify the situation here. It is good that you added the explanations. Bitwise OR is basically the same as bitwise addition without carry, but that's because addition is the bitwise or plus the carry mechanic. So they are kind of similar. Thus it is very important to distinguish between them, and not use the word addition for the bitwise or. – Dakkaron May 21 '15 at 08:55
  • 1
    @Dakkaron: couldn't put it in better words. Thanks for your input. :) – Amit May 23 '15 at 06:42