Don't you just love discovering new treasures?! Today I stumbled upon a Google/ex-Urchin javascript - urchin.js. Scrolling down the file with no apparent purpose in mind, I though I saw some >>
and <<
s. A second look? Yep, these do look like bitwise operators. In JavaScript? In the poor old insulted, sweared-upon, ignored, dumped to the juniors, forbidden, forgotten, no-good cheap status-bar-changing-only, blinking and blinding and obtrusive, cursor-following JavaScript. In the "abused, misunderstood, and kicked around like the poor step-child as known in fairytales" JavaScript (Dustin Diaz).
您不只是喜欢发现新的宝藏吗? 今天,我偶然发现了一个Google / ex-Urchin javascript- urchin.js 。 在没有明显目的的情况下向下滚动文件,尽管我看到了一些>>
和<<
。 再看看? 是的,这些看起来确实像按位运算符。 在JavaScript中? 在可怜的老侮辱,脏话,无视,丢到初级用户中,被禁止,被遗忘,仅凭廉价的状态栏更改,闪烁,盲目,引人注目JavaScript跟随。 在“被虐待,被误解并像童话故事中已知的可怜的继子一样踢来踢去” JavaScript( Dustin Diaz )中。
Yes, in (the reborn, beloved, the J in AJAX) Javascript, you can do bitwise operations, just like our grand-grand-dads did it in ANSI C 😉 It's questionably useful, but who knows, life and projects come in all shapes. Personally I can't remember the last time I needed to bother with bitwise operations. (Ohm yes, I do, two days ago, working on an ANSI C to Java migration project 🙁 )
是的,在(重生,倍受青睐的AJAX中的Java)JavaScript中,您可以执行按位运算,就像我们的曾祖父在ANSI C中所做的那样。useful这确实有用,但谁知道,生命和项目无处不在形状。 就我个人而言,我不记得上一次需要麻烦的按位操作。 (是的,两天前,我正在做一个从ANSI C到Java的迁移项目🙁)
Here's an example script (inspired by the PHP manual), that takes a hexadecimal color value, such as ff0088, parses it as 16 bit integer and then uses bitwise shifting to get the red, green and blue values from it.
这是一个示例脚本(受PHP手册启发),该脚本采用十六进制颜色值(例如ff0088),将其解析为16位整数,然后使用按位移位从中获取红色,绿色和蓝色值。
var hex = 'ffaadd';
var rgb = parseInt(hex, 16); // value is 1675421
var red = (rgb >> 16) & 0xFF; // returns 255
var green = (rgb >> 8) & 0xFF; // 170
var blue = rgb & 0xFF; // 221
And here's a demo to experiment with .
这是一个可以试用的演示。
For the full list of available bitwise opearations (it's more or less the same as in PHP, Java, C++#>;-\) and for some more info, just do a search query.
有关可用按位运算的完整列表(与PHP,Java,C ++#>;-\差不多),以及更多信息,请执行搜索查询。
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息
翻译自: https://www.phpied.com/bitwise-operations-in-javascript/