• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Snippet] GetPowerOfTwo

I only wrote this snippet because I have multiple resources that are using the same code, and I don't find it cool to repeat the same code over and over again.
Besides, a powers of 2 list is always useful.

JASS:
/********************************
*
*   GetPowerOfTwo
*   v1.0.0.1
*   By Magtheridon96
*
*   - This resource functions as a repository
*     for values and code that has been, is and 
*     most likely will be repeated by a lot of 
*     coders. It is a list of all powers of 2.
*
*   API:
*   ----
*
*       constant function GetPowerOfTwo takes integer x returns integer
*           - This returns 2^x.
*           - If x = 31, this function will return 2^31 - 1.
*             2^31 cannot be represented in a 32-bit signed integer.
*
********************************/
library GetPowerOfTwo

    globals
        private integer array data
    endglobals
    
    private module potm
        private static method onInit takes nothing returns nothing
            local integer i = 0
            local integer t = 1
            loop
                set data[i] = t
                exitwhen i == 30
                set t = t * 2
                set i = i + 1
            endloop
            set data[31] = 2147483647
        endmethod
    endmodule
    private struct pots extends array
        implement potm
    endstruct
    
    constant function GetPowerOfTwo takes integer x returns integer
        return data[x]
    endfunction
    
endlibrary
 
Last edited:
The 31 can't be included inside the loop because it will be set to 2147483648, which overflows, and becomes -2147483648 D:

As for the usefulness of the static if, I'm not sure either.
I'll get rid of it in favor of the one that you would choose because both don't make much of a difference to me.
The second one uses less operations, but that shouldn't matter.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Oh yes overflow. But wait, that means you have an accuracy of 4,656612873077393 * 10 ^(-8) % for the last number.
It should be stated on documentation.

Also you must be kidding about the favors. It harldy needs something else than the loop.
Much more script size VS some nanoseconds won on map load >.<
Con >>> "Pro"
 
Top