我在哪里使用JavaScript中的按位运算符? [英] Where would I use a bitwise operator in JavaScript?
本文介绍了我在哪里使用JavaScript中的按位运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已阅读'什么是按位运算符?',所以我知道 按位运算符是但是我还不清楚如何使用它们。任何人都可以提供任何实际的例子,其中按位运算符在JavaScript中有用吗?
I've read 'what are bitwise operators?', so I know what bitwise operators are but I'm still not clear on how one might use them. Can anyone offer any real-world examples of where a bitwise operator would be useful in JavaScript?
谢谢。
只需深入研究 jQuery source 我发现了一些使用按位运算符的地方,例如:(只有&运算符)
Just digging into the jQuery source I've found a couple of places where bitwise operators are used, for example: (only the & operator)
// Line 2756:
event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
// Line 2101
var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
推荐答案
示例:
解析十六进制值以获取RGB颜色值。
Parses hexadecimal value to get RGB color values.
var hex = 'ffaadd';
var rgb = parseInt(hex, 16); // rgb is 16755421
var red = (rgb >> 16) & 0xFF; // returns 255
var green = (rgb >> 8) & 0xFF; // 170
var blue = rgb & 0xFF; // 221
这篇关于我在哪里使用JavaScript中的按位运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文