• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[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