tags:

views:

485

answers:

6

I've read this (http://stackoverflow.com/quest...), 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;
+4  A: 

Given Ajax, we can now see more and more complex code in JS. Here are a couple of instances where I have used bitwise operators:

  1. You could do some fast ip adress operations.

    //computes the broadcast address based on the mask and a host address
    broadcast = (ip & mask) | (mask ^ 0xFFFFFFFF)
    
    
    //converts a number to an ip adress 
    sprintf(ip, "%i.%i.%i.%i", ((ip_int >> 24) & 0x000000FF),
                             ((ip_int >> 16) & 0x000000FF),
                             ((ip_int >>  8) & 0x000000FF),
                             ( ip_int        & 0x000000FF));
    

Note: this is C code, but JS is almost identical

  1. CRC algorithms uses them a lot

Check out the wikipedia entry on this

  1. Screen resolution operations
Bogdan Gavril
Do you have examples of you used bitwise operators in these situations?
thomasrutter
+2  A: 

You can use them for flipping a boolean value:

var foo = 1;
var bar = 0;
alert(foo ^= 1);
alert(bar ^= 1);

This is a bit silly though and for the most part bitwise operators do not have many applications in Javascript.

Andrew Hare
That's not silly. I use that to cycle through "on"/"off" states for images, divs, etc all the time.
Crescent Fresh
+4  A: 

Bitmasks.

Used extensively, for example, in JS events.

Yuval A
+7  A: 

Example:

Parses hexadecimal value to get RGB color values.

var hex = 'ffaadd';
var rgb = parseInt(hex, 16); 

var red   = (rgb >> 16) & 0xFF;
var green = (rgb >> 8) & 0xFF;  
var blue  = rgb & 0xFF;
Mark Robinson
A: 

I've used it once for a permissions widget. File permissions in unix are a bitmask, so to parse it, you need to use bit operations.

troelskn
+1  A: 

In JavaScript, you can use a double bitwise negation (~~n) as a replacement for Math.floor(n) (if n is a positive number) or parseInt(n, 10) (even if n is negative). n|n and n&n always yield the same results as ~~n.

var n = Math.PI;
n; // 3.141592653589793
Math.floor(n); // 3
parseInt(n, 10); // 3
~~n; // 3
n|n; // 3
n&n; // 3

// ~~n works as a replacement for parseInt() with negative numbers…
~~(-n); // -3
(-n)|(-n); // -3
(-n)&(-n); // -3
parseInt(-n, 10); // -3
// …although it doesn’t replace Math.floor() for negative numbers
Math.floor(-n); // -4

A single bitwise negation (~) calculates -(parseInt(n, 10) + 1), so two bitwise negations will return -(-(parseInt(n, 10) + 1) + 1).

It should be noted that of these three alternatives, n|n appears to be the fastest.

(As posted on http://stackoverflow.com/questions/1995113/strangest-language-feature/2019171#2019171)

Mathias Bynens