GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Programming Problem - Help (https://gfy.com/showthread.php?t=309309)

Dusen 06-08-2004 07:56 AM

Programming Problem - Help
 
I'm doing somethere where a selected value HAS to be an integer power of 2. Anyone know a "clean" way I could round a desired value to the nearest power of 2?

I.e. input is 37 -> rounded to 32

input is 84 - > rounded to 64

input is 99 -> rounded to 128


Just a general algo is fine, i can adapt it to PHP.

blazin 06-08-2004 08:20 AM

think in binary

jawanda 06-08-2004 08:24 AM

Alright, this might not be the cleanest approach, by try something like this.


A=2

While A is less than Input

A = A*2

Wend

Edit: Input = A/2

Ash@phpFX 06-08-2004 08:28 AM

Quote:

Originally posted by blazin
think in binary
:thumbsup

Dusen 06-08-2004 09:38 AM

Quote:

Originally posted by jawanda
Alright, this might not be the cleanest approach, by try something like this.


A=2

While A is less than Input

A = A*2

Wend

Edit: Input = A/2

This is currently what I'm doing, except after that I determine the distance between the value before and after the > value, then get the distance and choose it.

blazin: Thanks, I will :D


All times are GMT -7. The time now is 03:06 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123