• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

BitOperations

Status
Not open for further replies.
This is my humble bit operation library. Commented inside. Please comment. :D

JASS:
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:
Level 29
Joined
Jul 29, 2007
Messages
5,174
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).
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
539
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.
 
Status
Not open for further replies.
Top