1

I am having a hard time googling because of the symbols.

What do num & 8, num >> 8 and num >> 8 & 64 mean in javascript?

Huang
  • 1,919
  • 3
  • 15
  • 11
  • 1
    Don't look for the trees. Look for the forest. In this case, you are looking for "javascript operators" (of which there are numerous tables, from whence the correct operator can be looked up). –  Dec 01 '11 at 09:58
  • For links: http://stackoverflow.com/questions/654057/where-would-i-use-a-bitwise-operator-in-javascript, http://stackoverflow.com/questions/5324761/bit-tweaking-in-javascript, http://stackoverflow.com/questions/3734451/what-does-mean-in-javascript –  Dec 01 '11 at 09:59
  • possible duplicate of [What do ">>" and "<<" mean in Javascript?](http://stackoverflow.com/questions/6997909/what-do-and-mean-in-javascript) –  Dec 01 '11 at 10:02

3 Answers3

3

They are bitwise operators

lucapette
  • 20,564
  • 6
  • 65
  • 59
1

to get the hundreds digit [num]->[/ 100]->[% 10]

To extract bits, use the [&] object to mask out the bit you're interested in, then optionally bit shift with [>>].

For instance, to get bit 3 (counting from right starting with zero) [num]->[& 8]->[>> 3].

If you just want to use the calculation to control a logic object (like if or a ggate), you can skip the bit shift.

These are really not Max-specific questions, they are fundamental DP techniques.

Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
1

Shift left <<

The following expression means, shift 2 binary numbers to left:

alert( 1 << 2 );

So binary this is (I'll take 8 bit for example), shift the bits to left, 2 places

00000001 = 1
00000010 = 2
00000100 = 4

Shift right >>

The following expression means, shift 2 binary numbers to left:

alert( 255 >> 2 );

So binary this is (I'll take 8 bit for example), shift the bits to right, 2 places

11111111 = 255
01111111 = 127
00111111 = 63

AND operator &

That's the and operator

So you got the following rules:

11110011 = 243
01001111 = 79
01000011 = 67 = RESULT

Which means, only the 1 numbers will stay in the result if both of them have 1's on the same position.

Niels
  • 48,601
  • 4
  • 62
  • 81