Friday, February 23, 2024
171
rated 0 times [  178] [ 7]  / answers: 1 / hits: 35236  / 15 Years ago, tue, march 17, 2009, 12:00:00

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;

More From » bitwise-operators

 Answers
61

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

[#99832] Thursday, March 12, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sherryd

Total Points: 254
Total Questions: 92
Total Answers: 89

Location: Equatorial Guinea
Member since Sun, Feb 14, 2021
3 Years ago
;