participate


Java Programming [Archive] - Rounding up an int to a power of two
<<   Back to Forum  |   Give us Feedback
7 Duke Stars available
This topic has 16 replies on 2 pages.    1 | 2 | Next »
mannionh
Posts:11
Registered: 3/24/00
Rounding up an int to a power of two   
Apr 25, 2002 10:06 AM

 
Hi!
Could anyone give me a good method to round an int up to the nearest power of two. I'm working on a buddy system memory allocation program.
Thanks!!
 
limeybrit9
Posts:2,110
Registered: 8/3/99
Re: Rounding up an int to a power of two   
Apr 25, 2002 10:22 AM (reply 1 of 16)  (In reply to original post )

 
public int round(int i)
{
    if(i%2 != 0) i++;
 
    return i;
 
}
This will take in an int and test if it's evenly divisible by two. If not, it's gonna increment it by one. If it already is, well then it's already rounded to the closest power of two.
 
limeybrit9
Posts:2,110
Registered: 8/3/99
Re: Rounding up an int to a power of two   
Apr 25, 2002 10:23 AM (reply 2 of 16)  (In reply to #1 )

 
Ack... sorry.. I completely misread that. I was thinking multiple of two. That would be easier ;)
 
limeybrit9
Posts:2,110
Registered: 8/3/99
Re: Rounding up an int to a power of two   
Apr 25, 2002 10:27 AM (reply 3 of 16)  (In reply to #2 )

 
public int round(int i)
{
 
int power = 2;
while(i > power)
{
     power *= 2;
}
 
return power;
 
}
Try that... seems like it should work to me.
 
mannionh
Posts:11
Registered: 3/24/00
Re: Rounding up an int to a power of two   
Apr 25, 2002 10:32 AM (reply 4 of 16)  (In reply to #3 )

 
what does *= do?? I have never seen that construct before.
 
zneroladivad
Posts:759
Registered: 8/30/01
Re: Rounding up an int to a power of two   
Apr 25, 2002 10:50 AM (reply 5 of 16)  (In reply to #4 )

 
use java.lang.Integer

Sting bits = Integer.toBinaryString();
int power = bits.length() - bits.indexOf("1");
int answer = 2 * power;
 
limeybrit9
Posts:2,110
Registered: 8/3/99
Re: Rounding up an int to a power of two   
Apr 25, 2002 11:05 AM (reply 6 of 16)  (In reply to #4 )

 
= works just like += ... a = b is just a shorthand notation for a = a*b
 
Virum
Posts:1,511
Registered: 12/18/01
Re: Rounding up an int to a power of two   
Apr 25, 2002 11:26 AM (reply 7 of 16)  (In reply to #6 )

 
a = 2

It is read something like

a times by two and assigned new value.

= times/multipilcation

= = assign.


So if you ever se a combination like that again, you'll know what it means.


-= = minus and assign resulting value
= = add and assign resulting value
/= = divide and assign resulting value
= = multiply and assign resulting value

-- = decrease int by one and assign resulting value.
+
= increase by one and assign resulting value

<< = shift binary value x position to the right

EXAMPLE:

8 = 1000
2 = 10

8 << 2 = 2

BECAUSE:

1000 binary shifted to difits left (means all digits left are deleted) is 10


= works the opposite way as <<. (8 >> 1 = 64)

That is a brief overview of all operators. I was just going to explain the
= but I got to carried away! ;^)

Virum
 
jcraig_tmi
Posts:14
Registered: 9/21/01
Re: Rounding up an int to a power of two   
Apr 26, 2002 6:25 AM (reply 8 of 16)  (In reply to #5 )

 
Neither of the suggested solutions will work for negative numbers or for integers greater than 2^30. If you can constrain the problem in that way, then either will work fine, however in znerladivad, the final line should be
int answer = Math.pow(2,power)
rather than using multiplication. The String solution is nifty, but should have a bit of documentation to explain why it is that you're changing the int to a string.
We know that if you left shift the int by 1 bit, then the answer is between the input and the result.
int upperBound = intVal << 1;
but I can't see how to determine the exact value.
 
jummoMa1
Posts:255
Registered: 6/11/00
Re: Rounding up an int to a power of two   
Apr 26, 2002 7:45 AM (reply 9 of 16)  (In reply to #8 )

 
Does it look better?
long answer;
int i=Integer.parseInt(args[0]);
if(i<-1) {
 answer=(long)(Math.pow(2,Double.NEGATIVE_INFINITY));
} else {
 String bits=Long.toBinaryString((long)i);
 int exp=bits.length()-bits.indexOf("1");
 long upper=(long)Math.pow(2,exp);
 long lower=(long)Math.pow(2,exp-1);
 answer=(upper-i)>(i-lower)?lower:upper;
}
 
Abuse
Posts:2,819
Registered: 7/10/01
Re: Rounding up an int to a power of two   
Apr 26, 2002 7:48 AM (reply 10 of 16)  (In reply to #9 )

 
a version that doesn't use String(or even long :~/ ) would be nice... I'll have a go 2night.

rob,
 
jummoMa1
Posts:255
Registered: 6/11/00
Re: Rounding up an int to a power of two   
Apr 26, 2002 7:49 AM (reply 11 of 16)  (In reply to #9 )

 
ooops....
long answer;
int i=Integer.parseInt(args[0]);
if(i<0) { // error, was -1
  answer=0;
  // of course, it's the same of the (formally more correct)
  // answer=(long)(Math.pow(2,Double.NEGATIVE_INFINITY));
} else {
  String bits=Long.toBinaryString((long)i);
 int exp=bits.length()-bits.indexOf("1");
 long upper=(long)Math.pow(2,exp);
 long lower=(long)Math.pow(2,exp-1);
 answer=(upper-i)>(i-lower)?lower:upper;
}
 
 
jummoMa1
Posts:255
Registered: 6/11/00
Re: Rounding up an int to a power of two   
Apr 26, 2002 8:02 AM (reply 12 of 16)  (In reply to #10 )

 
Not using strings... and if you are not afraid of overflows, neither longs :-)
int answer;
if(i<0) answer=0;
else {
 int exp=0;
 for(int iTmp=i; (iTmp>>=1) >0; exp++);
 int upper=1<<exp+1;
 int lower=1<<exp;
 answer=(upper-i)>(i-lower)?lower:upper;
}
 
zneroladivad
Posts:759
Registered: 8/30/01
Re: Rounding up an int to a power of two   
Apr 26, 2002 10:30 AM (reply 13 of 16)  (In reply to #12 )

 
I was trying to be clever and I was close.

I was thinking there was a quick answer with a mask in there some how, but haven't figured it out. And-ing a simple mask should make everything all ones and add 1 to that value and you've got it.
 
zneroladivad
Posts:759
Registered: 8/30/01
Re: Rounding up an int to a power of two   
Apr 26, 2002 10:47 AM (reply 14 of 16)  (In reply to #13 )

 
Disregard that last entry....
 
This topic has 16 replies on 2 pages.    1 | 2 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums