Programming Problem - Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dusen
    Confirmed User
    • Aug 2002
    • 2251

    #1

    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.
    Penis Pill Sponsor List and Reviews:
    http://www.pillsponsors.com
  • blazin
    Confirmed User
    • Aug 2002
    • 2781

    #2
    think in binary
    I don't endorse a god damn thing......

    Comment

    • jawanda
      Confirmed User
      • Feb 2003
      • 6040

      #3
      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

      Comment

      • Ash@phpFX
        Confirmed User
        • Nov 2003
        • 4292

        #4
        Originally posted by blazin
        think in binary

        Comment

        • Dusen
          Confirmed User
          • Aug 2002
          • 2251

          #5
          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
          Penis Pill Sponsor List and Reviews:
          http://www.pillsponsors.com

          Comment

          Working...