Reputation: 37464
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!
Upvotes: 12
Views: 14591
Reputation: 324620
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".
Upvotes: 20
Reputation: 44316
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
Upvotes: 4
Reputation: 11128
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)
Upvotes: 2
Reputation: 224886
>>
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.
Upvotes: 8
Reputation: 7326
Looks like bitwise operators to me:
http://web.eecs.umich.edu/~bartlett/jsops.html
Edit: that ascii array was a dead give away... LOL
Upvotes: 1