0

I have trouble understanding "&" operator in javascript. Why does this code

var wins = [7, 56, 448, 73, 146, 292, 273, 84];
var score=77;
for (i = 0; i < wins.length; i += 1) {
    alert((wins[i] & score)===wins[i])

        }

return true on win[3] , basicly why does

73&77==73

I just cant find the right answers.

Darlyn
  • 4,152
  • 11
  • 30
  • 73
  • Also: http://stackoverflow.com/q/276706/497356 – Andrew Whitaker Jun 10 '15 at 17:00
  • 1
    What else would you have expected? – Bergi Jun 10 '15 at 17:00
  • 3
    `&` is "bitwise and" which means it's looking at the corresponding bits in the two values, and "and-ing" them (`1 & 1 = 1`, `1 & 0 = 0`, `0 & 1 = 0`, and `0 & 0 = 0`). If you translate 73 and 77 to binary, you get: `1001001 & 1001101` which has the result `1001001` (*bitwise* means you "and" each corresponding bit), that is, `73 & 77 === 73` is true. – lurker Jun 10 '15 at 17:01
  • 2
    Sounds like it's time you read up on [binary logical operations](http://en.wikipedia.org/wiki/Bitwise_operation). – tadman Jun 10 '15 at 17:01

0 Answers0