Jump to content

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


Recommended Posts

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:

https://github.com/The-Dialga-Team-Project-Zomboid-Mods/The-Dialga-Team-Mod-Collection/blob/master/mods/TDTModAPI/media/lua/shared/TheDialgaTeam/TDTModAPI/System/Bitwise.lua

 

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

https://github.com/The-Dialga-Team-Project-Zomboid-Mods/The-Dialga-Team-Mod-Collection/blob/master/mods/TDTModAPI/media/lua/shared/TheDialgaTeam/TDTModAPI/Lua/Table.lua

 

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

Spoiler

--- Boolean flag: false, true.

Bitwise.Types.bool

 

--- Unsigned Byte: 0, 255.

Bitwise.Types.byte

 

--- Signed Byte: -128, 127.

Bitwise.Types.sbyte

 

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

Bitwise.Types.short

 

--- Unsigned Short Integer: 0, 65535.

Bitwise.Types.ushort

 

--- Signed Integer: -2147483648, 2147483647.

Bitwise.Types.int

 

--- Unsigned Integer: 0, 4294967295.

Bitwise.Types.uint

 

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

Bitwise.Types.long

 

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

Bitwise.Types.ulong

 

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 "Bitwise.Types.int" 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, Bitwise.Types.int));
print(test);
-- Expected Result: -6

-- Test Case 2:
local test2 = Bitwise.Not(5); -- Automatically resolved as a Byte type which is 0 to 255 only.
print(test2);
-- 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.Not(binaryTable)

 

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

Bitwise.ConvertToNumericValue(binaryTable)

 

--- Trim padded zeros in the binary bits.

Bitwise.TrimPaddedZeros(binaryTable)

 

=============================================================================

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
Link to comment
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
×
×
  • Create New...