### Thread: Smallest Power of 2

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
8
Rep Power
0

#### Smallest Power of 2

I have to create a program that when the user enters a number greater than or equal to zero. The program will return the smallest power of two that is greater than or equal to that number.

I have to use a while statement. How do I go about doing this?

I already have an if statement displaying an error message if the user inputs a number that is less than 0.
2. That seems like an easy enough problem.
What are your ideas on how to solve it?
What are your problems?
Can you generate powers of 2? 1 2 4 8 ...
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
8
Rep Power
0
Its just when a user enters a number, I just need to return the smallest power of two for that number. I was given this but dont quite understand how to use it. Its broken code as I know im missing things.

int x =0;

while(Math.pow(x,2) < num){ x++

for (in x=0; x<num;x++){math.pow(x,2) >=num){ System.out.println(x);break;
4. Can you explain your algorithm for solving the problem in English?
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2010
Posts
8
Rep Power
0
Our java lab teacher gave it to us. I'm not sure but looking at it I can only guess that it means that if the x int is less than the number given then the ++ means add two to it? But i dont see how that would make sense.
6. No, not at all. x++ means add one to x. It's equivalent to x = x+1;

If you were going to solve this problem in English, how would you do it? How about "I would take each ascending power of two, and compare it to the inputted number to see if it is greater than or equal to it. And once that's true, I would be done."

Now you have a logical direction for your program. It's a fairly simple matter of converting this to code. If going directly there is too advanced at this moment, try doing pseudo code first, such as "loop for each power of two, while it's less than num" something like that. You get the idea - it's a series of steps to break a large problem down into a smaller one.

Note: It'll probably be better if you ignore the code your TA gave you for now, since you probably haven't been to class... -_-
7. #### Perhaps using logarithm?

Maybe using natural logarithm is better in this case.

1. set base = log (2)
2. set value = log(number)
3. set nearest = floor (value / base)
4. set answer = 2^nearest
8. Forget about writing the code for now. You need to design the program BEFORE you write the code.
Can you explain your algorithm for solving the problem in English?
Step one,
step two,
step three,
etc until you've got the answer
9. what i suggest is the following Algo

> a loop that iterates from i=1 to <entered_number> step by 1
> Look for Math Class pow() , so in that loop you will check for condition whether
pow(2,i <current_value in loop>) > <entered_number> or == <entered_number>
> when condition gets true then thats it ... you get
Originally Posted by dannyping
smallest power of two that is greater than or equal to that number.
EDIT:
Also when condition gets true don't forget to break loop, iterating further will be useless as you got the answer...

#### Comments on this post

• spyder0101 disagrees : Teach a man to fish...
• f_nietzsche agrees : No love for codeJ, regardless?
Last edited by codeJ; September 27th, 2010 at 09:05 AM.
10. codeJ
You're spoiling it for the OP. How is he going to learn if someone gives him the answers? At least let the OP try to find the algorithm before you give it.
You don't learn without trying. Reading someone else's work is not the same thing.
11. #### Apology

Sorry, was reading the post in hurry, suggested a different algorithm instead.

Basically you need to think of the loop as a sentinel, to ensure that the loop stops when the power of 2 is greater than the current number. You will have to keep on calculating the power of 2 from scratch until you found an answer.

All the nuts and bolts are in the codes provided by your instructor, but it is not quite in the right sequence and there are some syntax errors here and there. Try sorting out a systematic step-by-step way of how you would solve this problem, involving a loop.

Are you stucked at the Java syntax or are you having problem getting that algorithm provided by your instructor?
12. Originally Posted by NormR
You're spoiling it for the OP
was just helping ...
Still i gave a short algo and made the OP to code by himself ....
13. Yeah, its hard to know how much to feed an OP to get the light to come on. Some take more, some less.
Wonder if the OP every figured it out.
14. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jul 2005
Location
Bay Area, California
Posts
841
Rep Power
1686
An elegant and efficient approach to this problem is to use binary. It is sometimes better to understand how the computer works than just mathematics, because doing so can lead to impressive performance gains. I'll describe how I'd calculate the positive power of two (no two's compliment complexity)

A positive power of two has trailing zeros after the most-significant bit (MSB). For example,
2^0 = 0001
2^1 = 0010
2^2 = 0100
2^3 = 1000

First we determine if the input is a power of two by calculating the population count. This should be one. Integer#bitCount() is our friend here.

If its not, then we need to determine the MSB of our input, which would give us the next smallest power of two. Here Integer#highestOneBit() is handy.

With that we can perform a left shift on the value 1 to give us the next highest power. Integer#rotateLeft() is handy.

The code should be about as simple to write as other's suggested, but much faster.
Last edited by NovaX; October 2nd, 2010 at 02:55 PM.