Created
April 2, 2014 23:11
-
-
Save dgilperez/9945185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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