Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise XOR operator in JavaScript

Why is this:

console.log("1100" ^ "0001")
=> 1101 // as expected

console.log("1100" ^ "1001")
=> 1957 // ???

Please explain. Thanks.

like image 924
lamu Avatar asked Mar 31 '12 13:03

lamu


1 Answers

Those numbers are interpreted as decimal numbers.

Try:

console.log(parseInt("1100", 2) ^ parseInt("1001", 2))

Of course the answer (0101) is printed in decimal (5).

The JavaScript token grammar supports numbers in decimal, octal, and hex, but not binary. Thus:

console.log(0xC0 ^ 0x09)

The first one worked, by the way, because 1100 (decimal) is 1101 (decimal) after the xor with 1.

like image 194
Pointy Avatar answered Nov 18 '22 14:11

Pointy