admin管理员组文章数量:1290425
Why is this:
console.log("1100" ^ "0001")
=> 1101 // as expected
console.log("1100" ^ "1001")
=> 1957 // ???
Please explain. Thanks.
Why is this:
console.log("1100" ^ "0001")
=> 1101 // as expected
console.log("1100" ^ "1001")
=> 1957 // ???
Please explain. Thanks.
Share Improve this question edited Mar 31, 2012 at 14:11 Tomalak 338k68 gold badges546 silver badges635 bronze badges asked Mar 31, 2012 at 13:57 lamulamu 2153 silver badges8 bronze badges 1-
You are using the XOR operator, but actually you seem to want the OR, since for
1101 | 0001 = 1101
(OR), while1101 ^ 0001 = 0101
(XOR). – Tomalak Commented Mar 31, 2012 at 14:17
1 Answer
Reset to default 11Those 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.
本文标签: bit manipulationBitwise XOR operator in JavaScriptStack Overflow
版权声明:本文标题:bit manipulation - Bitwise XOR operator in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741496386a2381852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论