12

I saw this in some JS code:

        index = [
            ascii[0] >> 2,
            ((ascii[0] & 3) << 4) | ascii[1] >> 4,
            ((ascii[1] & 15) << 2) | ascii[2] >> 6,
            ascii[2] & 63
        ];

I'd quite like to know what a lot of this means. Specifically ">>", a single pipe "|" and the "&" symbol on the last line?

Much appreciated!

benhowdle89
  • 34,076
  • 63
  • 192
  • 314
  • 1
    (-1) Search for "JavaScript operators"... -1 because this is readily available information *and* is already far-too-commonly found on StackOverflow. –  May 02 '12 at 22:52
  • 2
    http://stackoverflow.com/questions/6194950/single-pipe-in-javascript , http://stackoverflow.com/questions/6997909/what-do-and-mean-in-javascript –  May 02 '12 at 22:53

5 Answers5

18

x >> y means to shift the bits of x by y places to the right (<< to the left).

x | y means to compare the bits of x and y, putting a 1 in each bit if either x or y has a 1 in that position.

x & y is the same as |, except that the result is 1 if BOTH x and y have a 1.

Examples:

#left-shifting 1 by 4 bits yields 16
1 << 4 = b00001 << 4 = b10000 = 16

#right-shifting 72 by 3 bits yields 9
72 >> 3 = b1001000 >> 3 = b1001 = 9

#OR-ing 
8 | 2 = b1000 | b0010 = b1010 = 10

#AND-ing
6 & 3 = b110 & b011 = b010 = 2

For more information, search Google for "bitwise operators".

kmario23
  • 42,075
  • 12
  • 123
  • 130
Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
8

>> is a right bitwise shift. It takes the bits and shifts them right n places1. For example, let's examine 35 >> 2:

35 = 100011 shift two places
     001000 = 8

And indeed, 35 >> 2 == 8.


| is a bitwise OR. It takes each bit in each operand and ORs them together. You can envision it as a sort of binary addition, but you don't carry when both top and bottom are 1. For example, here's 5 | 3:

5 = 101
3 = 011
| -----
    111 = 7

And indeed, 5 | 3 == 7.


Finally, & is a bitwise AND. It takes each bit in each operand, except instead of giving 1 if either one bit OR the other is one, it gives 1 if one bit AND the other are both one. For example, here's 5 & 3:

5 = 101
3 = 011
& -----
    001 = 1

Try it out; 5 & 3 == 1.


Some other ones you might want to be aware of are <<, which is a left bitwise shift, and ^, which is an XOR (0 when both bits are the same, 1 if they're different).

1 Actually, it's n modulo 32. 1 >> 32 is 1. Not sure why.

Ry-
  • 199,309
  • 51
  • 404
  • 420
4

The >> and << operators are a bitwise shift. For example,

11 =      00001011
11 << 3 = 01011000 = 88

It is worth noting that m << n = m * 2^n and m >> n = m / 2^n. This is sometimes used to do very efficient multiplication/division by powers of 2.

The & and | are bitwise and and or respectively.

11 =      00001011
28 =      00011100
11 & 28 = 00001000 = 8

11 =      00001011
28 =      00011100
11 | 28 = 00011111 = 31

While I'm at it, I should mention the ^ operator, which is not used for power, but for bitwise exclusive-or.

11 =      00001011
28 =      00011100
11 ^ 28 = 00010111 = 23
Kendall Frey
  • 39,334
  • 18
  • 104
  • 142
2
  • & (Bitwise AND)
  • | (Bitwise OR)
  • << (Left shift)
  • >> (Sign-propagating right shift)

Examples (from https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators):

Bitwise and:

     9 (base 10) = 00000000000000000000000000001001 (base 2)
    14 (base 10) = 00000000000000000000000000001110 (base 2)
                   --------------------------------
14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)

Left shift (9 << 2 shifts bits of 9 in binary, 2 bits to the left):

     9 (base 10): 00000000000000000000000000001001 (base 2)
                  --------------------------------
9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10)
German Latorre
  • 8,457
  • 11
  • 39
  • 59
1

Looks like bitwise operators to me:

http://web.eecs.umich.edu/~bartlett/jsops.html

Edit: that ascii array was a dead give away... LOL

Kris Krause
  • 7,134
  • 2
  • 20
  • 26