Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where would I use a bitwise operator in 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?

Thanks.

Edit:

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; 
like image 966
James Avatar asked Mar 17 '09 12:03

James


People also ask

What is bitwise and used for?

The bitwise AND operator ( & ) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Where is bitwise operator used?

Examples of uses of bitwise operations include encryption, compression, graphics, communications over ports/sockets, embedded systems programming and finite state machines. A bitwise operator works with the binary representation of a number rather than that number's value.


2 Answers

Example:

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   
like image 72
Mark Avatar answered Oct 07 '22 19:10

Mark


I heavily use bitwise operators for numerical convertions in production scripts, because sometimes they're much faster than their Math or parseInt equivalents.

The price I have to pay is code readability. So I usualy use Math in development and bitwise in production.

You can find some performance tricks on jsperf.com.

As you can see, browsers don't optimize Math.ceil and parseInt for years, so I predict bitwise will be faster and shorter way to do things in furure as well.

Some further reading on SO...


Bonus: cheat sheet for | 0 : an easy and fast way to convert anything to integer:

( 3|0 ) === 3;             // it does not change integers ( 3.3|0 ) === 3;           // it casts off the fractional part in fractionalal numbers ( 3.8|0 ) === 3;           // it does not round, but exactly casts off the fractional part ( -3.3|0 ) === -3;         // including negative fractional numbers ( -3.8|0 ) === -3;         // which have Math.floor(-3.3) == Math.floor(-3.8) == -4 ( "3"|0 ) === 3;           // strings with numbers are typecast to integers ( "3.8"|0 ) === 3;         // during this the fractional part is cast off too ( "-3.8"|0 ) === -3;       // including negative fractional numbers ( NaN|0 ) === 0;           // NaN is typecast to 0 ( Infinity|0 ) === 0;      // the typecast to 0 occurs with the Infinity ( -Infinity|0 ) === 0;     // and with -Infinity ( null|0 ) === 0;          // and with null, ( (void 0)|0 ) === 0;      // and with undefined ( []|0 ) === 0;            // and with an empty array ( [3]|0 ) === 3;           // but an array with one number is typecast to number ( [-3.8]|0 ) === -3;       // including the cast off of the fractional part ( [" -3.8 "]|0 ) === -3;   // including the typecast of strings to numbers ( [-3.8, 22]|0 ) === 0     // but an Array with several numbers is typecast to 0 ( {}|0 ) === 0;                // an empty object is typecast to 0 ( {'2':'3'}|0 ) === 0;         // or a not empty object ( (function(){})|0 ) === 0;    // an empty function is typecast to 0 too ( (function(){ return 3;})|0 ) === 0; 

and some magic for me:

3 | '0px' === 3; 
like image 30
Dan Avatar answered Oct 07 '22 19:10

Dan