1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  4. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  5. Units have been turned into heroes in our latest Icon Contest! Vote for the best icon set at the Icon Contest #18 Poll!
    Dismiss Notice
  6. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  7. The raddest synthwave tracks were chosen - Check out our Music Contest #12 - Results and congratulate the winners!
    Dismiss Notice
  8. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

BitOperations

Discussion in 'The Lab' started by bowser499, Apr 24, 2015.

  1. bowser499

    bowser499

    Joined:
    Jul 20, 2009
    Messages:
    782
    Resources:
    7
    Tools:
    1
    Maps:
    3
    Spells:
    3
    Resources:
    7
    This is my humble bit operation library. Commented inside. Please comment. :D

    Code (vJASS):
    library BitOperations initializer Init
        globals
            // Warcraft 3 uses 32-bit signed integers.
            private constant integer       BITS_IN_INTEGER = 32
           
            private          integer array Pow2[BITS_IN_INTEGER]
        endglobals

        // BitwiseNot(a) = ~a in C++
        function BitwiseNot takes integer a returns integer
            return -1-x
        endfunction
       
        // LeftShift(a,bitCount) = a << bitCount in C++
        function LeftShift takes integer a, integer bitCount returns integer
            return a*Pow2[bitCount]
        endfunction
       
        // RightShift(a,bitCount) = a >> bitCount in C++
        function RightShift takes integer a, integer bitCount returns integer
            return a/Pow2[bitCount]
        endfunction
       
        // BitwiseAnd(a,b) = a&b in C++
        function BitwiseAnd takes integer a, integer b returns integer
            local integer aBit     // Bit of a;
            local integer bBit     // Bit of b;
            local integer rBit     // Bit of result
            local integer r    = 0 // result
           
            loop
                exitwhen i==BITS_IN_INTEGER
                set aBit = ModuloInteger(a, 2)
                set bBit = ModuloInteger(b, 2)
               
                if aBit == 1 and bBit == 1 then
                    set rBit = 1
                else
                    set Bit = 0
                endif
               
                set r = r + LeftShift(rBit,i)
               
                set a = a / 2
                set b = b / 2
                set i = i + 1
            endloop
           
            return r
        endfunction
       
        // BitwiseOr(a,b) = a|b in C++
        function BitwiseOr takes integer a, integer b returns integer
            local integer aBit     // Bit of a
            local integer bBit     // Bit of b
            local integer rBit     // Bit of result
            local integer r    = 0 // result
           
            loop
                exitwhen i==BITS_IN_INTEGER
                set aBit = ModuloInteger(a, 2)
                set bBit = ModuloInteger(b, 2)
               
                if aBit == 1 or bBit == 1 then
                    set rBit = 1
                else
                    set rBit = 0
                endif
               
                set r = r + LeftShift(rBit,i)
               
                set a = a / 2
                set b = b / 2
                set i = i + 1
            endloop
           
            return r
        endfunction
       
        // BitwiseXor(a,b) = a^b in C++
        function BitwiseXor takes integer a, integer b returns integer
            local integer aBit     // Bit of a
            local integer bBit     // Bit of b
            local integer rBit     // Bit of result
            local integer r    = 0 // result
           
            loop
                exitwhen i==BITS_IN_INTEGER
                set aBit = ModuloInteger(a, 2)
                set bBit = ModuloInteger(b, 2)
               
                if aBit != bBit then
                    set rBit = 1
                else
                    set rBit = 0
                endif
               
                set r = r + LeftShift(rBit,i)
               
                set a = a / 2
                set b = b / 2
                set i = i + 1
            endloop
           
            return r
        endfunction

        private function Init takes nothing returns nothing
            local integer i = 1
           
            // Pre-init the power of two array to speed up the system.
            set Pow2[0] = 1
            loop
                exitwhen i > BITS_IN_INTEGER-1
                set Pow2[i] = Pow2[i-1]*2
                set i = i + 1
            endloop    
        endfunction
    endlibrary
     
    Last edited: Apr 24, 2015
  2. GhostWolf

    GhostWolf

    Joined:
    Jul 29, 2007
    Messages:
    4,836
    Resources:
    2
    Tools:
    1
    Tutorials:
    1
    Resources:
    2
    You have syntax errors (cBit, rBit).
    Can you not use logical expressions as integers in Jass, to avoid those conditions? (I honestly don't remember).
    As to usefulness, it's obvious this has none, but then everything that is made these days follows this...

    Check this out (that's for 16bit integers, but I assume it can be applied with changes to 32bit ones).
     
  3. LeP

    LeP

    Joined:
    Feb 13, 2008
    Messages:
    446
    Resources:
    0
    Resources:
    0
    using a lookup table approach I'm certain you can't beat this.
    and I don't know a faster way than table based approaches.