# BitOperations

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

1. ### 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.

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

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

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.