Skip to content

Instantly share code, notes, and snippets.

@dgilperez
Created April 2, 2014 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgilperez/9945185 to your computer and use it in GitHub Desktop.
Save dgilperez/9945185 to your computer and use it in GitHub Desktop.
// Slick little method to create a bitmask from an array of boolean values. (for Javascript)
function bitMask(a, b) {
var m = 0, i;
if (b !== undefined) { a = Array.prototype.slice.apply(arguments); }
for (i = 0; i < a.length; i += 1) {
if (a[i] !== false) { m += Math.pow(2, i) };
}
return m;
}
# Slick little method to create a bitmask from an array of boolean values. (for Ruby)
def bitMask a
n = -1
b = a.map { |x| 2**(n+=1) * (x ? 1 : 0) }
b.inject { |s, x| s += x }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment