Please somebody tell me I'm being blind.
int Function bitwiseAnd8 (int i1, int i2) Global;quick hackjob - I miss SKSE already;would probably be better in a loopReturn \((i1 - i1 % 128) / 128 * (i2 - i2 % 128) / 128) * 128 + \((i1 % 128 - i1 % 64) / 64 * (i2 % 128 - i2 % 64) / 64) * 64 + \((i1 % 64 - i1 % 32) / 32 * (i2 % 64 - i2 % 32) / 32) * 32 + \((i1 % 32 - i1 % 16) / 16 * (i2 % 32 - i2 % 16) / 16) * 16 + \((i1 % 16 - i1 % 8) / 8 * (i2 % 16 - i2 % 8) / 8) * 8 + \((i1 % 8 - i1 % 4) / 4 * (i2 % 8 - i2 % 4) / 4) * 4 + \((i1 % 4 - i1 % 2) / 2 * (i2 % 4 - i2 % 2) / 2) * 2 + \(i1 % 2) * (i2 % 2)EndFunction
Int Function bitwiseOp(Int i1, Int i2, Int iBits = 31, Int iOp = 1, Bool bWarn = False) Global{31 bitwise operations. Returns a negative number on errorsSet iBits lower to limit the bitmask to the lower bits for efficiency - Def = 31bitsSet bOp for the bitwise operation. 0 = NOT, 1 = AND(default), 2 = OR, 3 = XORSet bWarn to True if you are too lazy to check the error return value, and want a notification}int iRes = 0 ; Accumulated resultint iDiv;iBits Sanity checksIf iBits < 1 Return 0EndIfIf iBits > 31 iRes = -3 ; iBits too highElseIf i1 < 0 iRes = -1 ; 1st param -veElseIf iOp && (i2 < 0) iRes = -2 ; 2nd param -veEndIfIf iRes != 0 If bWarn Debug.Notification("PARAMETER ERROR in function bitwiseOp. Code = " + iRes ) EndIf Return iResEndIf; Throw away the upper bitsIf iBits != 31 int i = Math.Pow(2, iBits) As Int i1 %= i i2 %= iEndIf; Init divisor to loop from MSBsiDiv = Math.Pow(2, (iBits - 1)) As IntIf iOp == 1 ; AND If !i1 || !i2 Return 0 EndIf While(iBits) If ((i1 / iDiv) As Int) && ((i2 / iDiv) As Int) iRes += iDiv EndIf i1 %= iDiv i2 %= iDiv iDiv /= 2 iBits -= 1 EndWhileElseIf iOp == 2 ; OR If !i1 && !i2 Return 0 EndIf While(iBits) If ((i1 / iDiv) As Int) || ((i2 / iDiv) As Int) iRes += iDiv EndIf i1 %= iDiv i2 %= iDiv iDiv /= 2 iBits -= 1 EndWhileElseIf iOp == 3 ; XOR While(iBits) int a = i1 / iDiv int b = i2 / iDiv If (( a && !b ) || ( !a && B)) iRes += iDiv EndIf i1 %= iDiv i2 %= iDiv iDiv /= 2 iBits -= 1 EndWhileElseIf iOp == 0 ; NOT While(iBits) If !((i1 / iDiv) As Int) iRes += iDiv EndIf i1 %= iDiv iDiv /= 2 iBits -= 1 EndWhileElseIf bWarn Debug.Notification("PARAMETER ERROR in function bitwiseOp. Code = -4 (unknown iOp)") Return -4EndIfReturn iResEndFunction