Skip to content

Instantly share code, notes, and snippets.

@giuseppeg
Last active July 6, 2022 08:55
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save giuseppeg/5939177 to your computer and use it in GitHub Desktop.
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
// 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