{"id":96,"date":"2006-02-24T01:09:10","date_gmt":"2006-02-24T06:09:10","guid":{"rendered":"http:\/\/www.phpied.com\/bitwaise-operations-in-javascript\/"},"modified":"2006-02-24T01:09:43","modified_gmt":"2006-02-24T06:09:43","slug":"bitwise-operations-in-javascript","status":"publish","type":"post","link":"https:\/\/www.phpied.com\/bitwise-operations-in-javascript\/","title":{"rendered":"Bitwise operations in JavaScript"},"content":{"rendered":"<p>Don't you just love discovering new treasures?! Today I stumbled upon a Google\/ex-Urchin javascript - <a href=\"http:\/\/www.google-analytics.com\/urchin.js\">urchin.js<\/a>. Scrolling down the file with no apparent purpose in mind, I though I saw some <code>&gt;&gt;<\/code> and <code>&lt;&lt;<\/code>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 (<a href=\"http:\/\/www.dustindiaz.com\/json-for-the-masses\/\">Dustin Diaz<\/a>).<\/p>\n<p>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 \ud83d\ude09 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 \ud83d\ude41 )<\/p>\n<p>Here's an example script (inspired by the <a href=\"http:\/\/php.net\/imagecolorat\">PHP manual<\/a>), 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.<\/p>\n<pre><span class=\"hl-reserved\">var <\/span><span class=\"hl-identifier\">hex<\/span><span class=\"hl-default\"> = <\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">ffaadd<\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-default\">;\r\n<\/span><span class=\"hl-reserved\">var <\/span><span class=\"hl-identifier\">rgb<\/span><span class=\"hl-default\"> = <\/span><span class=\"hl-identifier\">parseInt<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">hex<\/span><span class=\"hl-code\">, <\/span><span class=\"hl-number\">16<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-default\">; <\/span><span class=\"hl-comment\">\/\/ value is 1675421\r\n\r\n\r\n<\/span><span class=\"hl-reserved\">var <\/span><span class=\"hl-identifier\">red<\/span><span class=\"hl-default\">   = <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">rgb<\/span><span class=\"hl-code\"> &gt;&gt; <\/span><span class=\"hl-number\">16<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-default\"> &amp; <\/span><span class=\"hl-number\">0<\/span><span class=\"hl-identifier\">xFF<\/span><span class=\"hl-default\">; <\/span><span class=\"hl-comment\">\/\/ returns 255\r\n<\/span><span class=\"hl-reserved\">var <\/span><span class=\"hl-identifier\">green<\/span><span class=\"hl-default\"> = <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">rgb<\/span><span class=\"hl-code\"> &gt;&gt; <\/span><span class=\"hl-number\">8<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-default\"> &amp; <\/span><span class=\"hl-number\">0<\/span><span class=\"hl-identifier\">xFF<\/span><span class=\"hl-default\">;  <\/span><span class=\"hl-comment\">\/\/ 170\r\n<\/span><span class=\"hl-reserved\">var <\/span><span class=\"hl-identifier\">blue<\/span><span class=\"hl-default\">  = <\/span><span class=\"hl-identifier\">rgb<\/span><span class=\"hl-default\"> &amp; <\/span><span class=\"hl-number\">0<\/span><span class=\"hl-identifier\">xFF<\/span><span class=\"hl-default\">;\t\t<\/span><span class=\"hl-comment\">\/\/ 221\t<\/span><\/pre>\n<p>And here's a <a href=\"https:\/\/www.phpied.com\/files\/bitwise\/bitwise.html\">demo to experiment with<\/a> .<\/p>\n<p>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 <a href=\"http:\/\/search.yahoo.com\/search?p=javascript+bitwise+operators\">search<\/a> <a href=\"http:\/\/www.google.ca\/search?q=javascript+bitwise+operators\">query<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Warning: geek post ahead with a practical use of &#8220;little to none&#8221;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/posts\/96"}],"collection":[{"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":0,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}