Bitwise operations (Not, And, Or, Xor, LShift, RShift)

2 posts in this topic

Recommended Posts

Posted (edited)

Update: Some functions have been refactored for easy reference.


I don't expect anyone will need this unless you are planning to do complex enumeration in lua.

I created Bitwise operations that does whatever it needs to get it done.


Original Source Code:


Dependent file: ( require "TheDialgaTeam/TDTModAPI/Lua/Table" )


Binary Table Types: (By default, it will choose the one that is the smallest possible type to reduce processing time)


--- Boolean flag: false, true.



--- Unsigned Byte: 0, 255.



--- Signed Byte: -128, 127.



--- Signed Short Integer: -32768, 32767.



--- Unsigned Short Integer: 0, 65535.



--- Signed Integer: -2147483648, 2147483647.


--- Unsigned Integer: 0, 4294967295.



--- Signed Long Integer: -9*10^18, 9*10^18.



--- Unsigned Long Integer: 0, 1.8*10^19.



How to get Binary Table?

Either use ConvertToBinaryTable(value, valueType) or use numeric values which will be automatically converted to binary table based on the smallest possible type.

Note: It may affect the returned value based on the type of binary table. If you are unsure, you can always use "" as the valueType parameter.


Example of a difference it can make:

local Bitwise = require "TheDialgaTeam/TDTModAPI/System/Bitwise";

-- Test Case 1:
local test = Bitwise.Not(Bitwise.ConvertToBinaryTable(5,;
-- Expected Result: -6

-- Test Case 2:
local test2 = Bitwise.Not(5); -- Automatically resolved as a Byte type which is 0 to 255 only.
-- Expected Result: 250

-- Signed and Unsigned bits makes a difference when using NOT operation. NOT operation does two's complement, depending on the type, it may end up negative or positive whole number.


--- Bitwise operation NOT (~)



--- Bitwise operation AND (&)

Bitwise.And(binaryTable, binaryTable2)


--- Bitwise operation OR (|)

Bitwise.Or(binaryTable, binaryTable2)


--- Bitwise operation XOR (^)

Bitwise.Xor(binaryTable, binaryTable2)


--- Bitwise operation LShift (<<)

Bitwise.LShift(binaryTable, value)


--- Bitwise operation RShift (>>)

Bitwise.RShift(binaryTable, value)


--- Convert numeric values into binary bits representation

Bitwise.ConvertToBinaryTable(value, valueType)

Return a table consist of: { type = "bool", minValue = 0, maxValue = 1, bits = 1, hasNegativeBits = false, value = { 0 } }


--- Convert binary bits into numeric representation.



--- Trim padded zeros in the binary bits.




Why is the default not integer when using numeric values for binaryTable?

This is because lua is dynamic and usually resolve them so generically. I tried to simulate the same thing in lua. I figured using a smaller type yields smaller bits and less computational time is needed for each operation.


If you notice, more bits require more loops. This is done to accurately calculate the actual bits within a specific constraint. I want to avoid looping too much as it may introduce unnecessary lag in-game.


After all, using AND or OR or XOR operation doesn't affect anything when dynamic resolve technique is used. Just bare in mind NOT operation does affect the results.

Edited by jianmingyong

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now