Archive for March, 2013

Bitwise Operators – JavaScript | MDN https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators

Example: Flags and bitmasks

The bitwise logical operators are often used to create, manipulate, and read sequences of flags, which are like binary variables. Variables could be used instead of these sequences, but binary flags take much less memory (by a factor of 32).

Suppose there are 4 flags:

• flag A: we have an ant problem
• flag B: we own a bat
• flag C: we own a cat
• flag D: we own a duck

These flags are represented by a sequence of bits: DCBA. When a flag is set, it has a value of 1. When a flag is cleared, it has a value of 0. Suppose a variable flags has the binary value 0101:

 1 var flags = 0x5;   // binary 0101

This value indicates:

• flag A is true (we have an ant problem);
• flag B is false (we don’t own a bat);
• flag C is true (we own a cat);
• flag D is false (we don’t own a duck);

Since bitwise operators are 32-bit, 0101 is actually 00000000000000000000000000000101, but the preceding zeroes can be neglected since they contain no meaningful information.

bitmask is a sequence of bits that can manipulate and/or read flags. Typically, a “primitive” bitmask for each flag is defined:

 1 2 3 4 var FLAG_A = 0x1; // 0001 var FLAG_B = 0x2; // 0010 var FLAG_C = 0x4; // 0100 var FLAG_D = 0x8; // 1000

New bitmasks can be created by using the bitwise logical operators on these primitive bitmasks. For example, the bitmask 1011 can be created by ORing FLAG_A, FLAG_B, and FLAG_D:

 1 var mask = FLAG_A | FLAG_B | FLAG_D; // 0001 | 0010 | 1000 => 1011

Individual flag values can be extracted by ANDing them with a bitmask, where each bit with the value of one will “extract” the corresponding flag. The bitmask masks out the non-relevant flags by ANDing with zeros (hence the term “bitmask”). For example, the bitmask 0100 can be used to see if flag C is set:

 1 2 3 4 // if we own a cat if (flags & FLAG_C) { // 0101 & 0100 => 0100 => true    // do stuff }

A bitmask with multiple set flags acts like an “either/or”. For example, the following two are equivalent:

 1 2 3 4 // if we own a bat or we own a cat if ((flags & FLAG_B) || (flags & FLAG_C)) { // (0101 & 0010) || (0101 & 0100) => 0000 || 0100 => true    // do stuff }
 1 2 3 4 5 // if we own a bat or cat var mask = FLAG_B | FLAG_C; // 0010 | 0100 => 0110 if (flags & mask) { // 0101 & 0110 => 0100 => true    // do stuff }

Flags can be set by ORing them with a bitmask, where each bit with the value one will set the corresponding flag, if that flag isn’t already set. For example, the bitmask 1100 can be used to set flags C and D:

 1 2 3 // yes, we own a cat and a duck var mask = FLAG_C | FLAG_D; // 0100 | 1000 => 1100 flags |= mask;   // 0101 | 1100 => 1101

Flags can be cleared by ANDing them with a bitmask, where each bit with the value zero will clear the corresponding flag, if it isn’t already cleared. This bitmask can be created by NOTing primitive bitmasks. For example, the bitmask 1010 can be used to clear flags A and C:

 1 2 3 // no, we don't neither have an ant problem nor own a cat var mask = ~(FLAG_A | FLAG_C); // ~0101 => 1010 flags &= mask;   // 1101 & 1010 => 1000

The mask could also have been created with ~FLAG_A & ~FLAG_C (De Morgan’s law):

 1 2 3 // no, we don't have an ant problem, and we don't own a cat var mask = ~FLAG_A & ~FLAG_C; flags &= mask;   // 1101 & 1010 => 1000

Flags can be toggled by XORing them with a bitmask, where each bit with the value one will toggle the corresponding flag. For example, the bitmask 0110 can be used to toggle flags B and C:

 1 2 3 4 // if we didn't have a bat, we have one now, and if we did have one, bye-bye bat // same thing for cats var mask = FLAG_B | FLAG_C; flags = flags ^ mask;   // 1100 ^ 0110 => 1010

Finally, the flags can all be flipped with the NOT operator:

 1 2 // entering parallel universe... flags = ~flags;    // ~1010 => 0101

Conversion snippets

Convert a binary string to a decimal number:

 1 2 3 var sBinString = "1011"; var nMyNumber = parseInt(sBinString, 2); alert(nMyNumber); // prints 11, i.e. 1011

Convert a decimal number to a binary string:

 1 2 3 var nMyNumber = 11; var sBinString = nMyNumber.toString(2); alert(sBinString); // prints 1011, i.e. 11

Automatize the creation of a mask

If you have to create many masks from some boolean values, you can automatize the process:

Reverse algorithm: an array of booleans from a mask

If you want to create an array of booleans from a mask you can use this code: