Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //fizzbuzz without mod, multiplication, division, or subtraction:
- #include <iostream>
- #include <iomanip>
- //constant for 15 bitshifts AKA binary => 1000 0000 0000 0000 (16 digits over)
- #define FIZZBUZZ 32768
- //constant for 5 bitshifts AKA binary => 0010 0000 (6 digits over)
- #define BUZZ 16
- //constant for 3 bitshifts AKA binary => 1000 (4 digits over)
- #define FIZZ 8
- int main()
- {
- //create your tracking variables prepped for bitshift
- short unsigned int fizzBuzzTracker = 1, buzzTracker = 1, fizzTracker = 1;
- //loop for 1000 itterations of fizzbuzz
- for (unsigned int i=0; i <= 1000;i++)
- {
- if (fizzBuzzTracker == FIZZBUZZ)//do this if we reached a fizzbuzz(iteration is divisible by 15, (i % 15) == 0)
- {
- std::cout << "Fizzbuzz!";//output fizzbuzz
- fizzBuzzTracker = buzzTracker = fizzTracker = 1;//reset all tracking varables
- }
- else if (buzzTracker == BUZZ)//do this if we reached a buzz(iteration is divisible by 5, (i % 5) == 0)
- {
- std::cout << "Buzz!";//output buzz
- buzzTracker = 1;//reset the buzz tracker variable
- }
- else if (fizzTracker == FIZZ)//do this if we reached a fizz(iteration is divisible by 3, (i % 3) == 0)
- {
- std::cout << "Fizz!";//output fizz
- fizzTracker = 1;//reset the fizz tracker variable
- }
- else//no special conditions were hit
- {
- //output the current iteration number
- std::cout << i;
- }
- std::cout << std::endl;//go to the next line
- //bitshift the tracker variables
- fizzBuzzTracker = fizzBuzzTracker << 1;
- buzzTracker = buzzTracker << 1;
- fizzTracker = fizzTracker << 1;
- }//end fizzbuzz for()
- std::cin.get();//pause
- return 0;
- }
- //alternate FizzBuzz: https://pastebin.com/XnZwE4DN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement