Last active
July 6, 2022 08:55
-
-
Save giuseppeg/5939177 to your computer and use it in GitHub Desktop.
FizzBuzz solution with just one comparison:Bitwise operations, using predefined 0-15 numbers masksource: http://www.zoharbabin.com/which-fizzbuzz-solution-is-the-most-efficient
This file contains hidden or 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
// FizzBuzz solution with one comparison: | |
// Bitwise operations, using predefined 0-15 numbers mask | |
// live demo: http://jsfiddle.net/TbAuQ/ | |
// source: http://www.zoharbabin.com/which-fizzbuzz-solution-is-the-most-efficient | |
var words = [undefined, "Fizz", "Buzz", "FizzBuzz"], | |
mask = 810092048, //11 00 00 01 00 10 01 00 00 01 10 00 01 00 00 | |
c = 0; | |
for (var i = 1;i <= 100; i += 1) { | |
c = mask & 3; | |
console.log( | |
i, | |
words[c] || i | |
); | |
mask = mask >> 2 | c << 28; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment