Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //fizzbuzz without mod, multiplication, division, or subtraction:
- #include <iostream>
- #include <iomanip>
- int main()
- {
- ///these constants are checked against the trackers
- //constant for 15 bitshifts AKA binary => 1000 0000 0000 0000 (15 digit intervals with an offset of 1)
- const short unsigned int FIZZBUZZ = 0x8000;
- //this handles offsets of 15 for 5 and 10. Multiples of 15 is handled via FIZZBUZZ
- //constant for 5 bitshifts interval(bitmask) AKA binary =>0000 0100 0010 0000 (5 digit intervals with an offset of 1)
- const short unsigned int BUZZ = 0x0420;
- //this handles offsets of 15 for 3, 6, 9, and 12. multiples of 15 is handled via FIZZBUZZ
- //constant for 3 bitshifts AKA binary => 0001 0010 0100 1000 (3 digit intervals with an offset of 1)
- const short unsigned int FIZZ = 0x1248;
- ///end of constants
- ///other variables
- //create your tracking variables prepped for bitshift
- short unsigned int fizzBuzzAndBuzzTracker = 1;
- ///end variables
- ///Start FizzBuzzing
- //loop for 1000 itterations of fizzbuzz
- for (unsigned int i=0; i <= 1000;i++)
- {
- if (fizzBuzzAndBuzzTracker & FIZZBUZZ)//do this if we reached a fizzbuzz(iteration is divisible by 15, (i % 15) == 0)
- {
- std::cout << "Fizzbuzz!";//output fizzbuzz
- fizzBuzzAndBuzzTracker = 1;//reset all tracking varables
- }
- else if (fizzBuzzAndBuzzTracker & BUZZ)//do this if we reached a buzz(iteration is divisible by 5, (i % 5) == 0)
- {
- std::cout << "Buzz!";//output buzz
- }
- else if (fizzBuzzAndBuzzTracker & FIZZ)//do this if we reached a fizz(iteration is divisible by 3, (i % 3) == 0)
- {
- std::cout << "Fizz!";//output fizz
- }
- 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
- fizzBuzzAndBuzzTracker = fizzBuzzAndBuzzTracker << 1;
- }//end fizzbuzz for()
- std::cin.get();//pause
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment