I have an assignment to take a number from the user, and then see what the smallest power of two that starts with the same number.
For example: if you entered 51, the first power of 2 would be 9 (2^9 = 512).
So I'm trying to do it in C++, but I don't know how to compare the user input to each power of 2 that is outputted. In other words, lets say you entered 51, how do I look at only the first 2 digits of the output of my power of two function to see if they're equal to 51?
I already came up with a way of doing it but I know it cannot be the most efficient way.
I divide the user input by 10 until it's equal to 0 (integer division) to find the number of digits in the input number (lets call it numOfPlaces). Then I do the same divide by 10 for each output of the power of two to find it's length (lets call it powOfTwoPlaces). Then again, I divide the power of two by 10 powOfTwoPlaces - numOfPlaces times. Then I compare the original input with the remaining numOfPlaces length power of two.
It's just so terribly ineffecient...I'd really like to skip all that busy work for each power of two!
For example: if you entered 51, the first power of 2 would be 9 (2^9 = 512).
So I'm trying to do it in C++, but I don't know how to compare the user input to each power of 2 that is outputted. In other words, lets say you entered 51, how do I look at only the first 2 digits of the output of my power of two function to see if they're equal to 51?
I already came up with a way of doing it but I know it cannot be the most efficient way.
I divide the user input by 10 until it's equal to 0 (integer division) to find the number of digits in the input number (lets call it numOfPlaces). Then I do the same divide by 10 for each output of the power of two to find it's length (lets call it powOfTwoPlaces). Then again, I divide the power of two by 10 powOfTwoPlaces - numOfPlaces times. Then I compare the original input with the remaining numOfPlaces length power of two.
It's just so terribly ineffecient...I'd really like to skip all that busy work for each power of two!