Open dominik-korsa opened 1 month ago
I'm willing to implement this, either as part of The Modpack or as a separate mod. I do however believe that this change fits The Modpack and could be implemented here trivially, while creating a separate mod would lead to more fragmentation and be unnecessarily cumbersome.
Description
Add bitwise operations to math mode blocks:
&
bitwise and|
bitwise or^
bitwise xorand optionally:
~
bitwise not (this might be tricky with Lua's doubles)<<
bitshift left (multiplying by powers of 2)>>
bitshift right (integer division by powers of 2)Advantages
This would be helpful for storing many states compactly and managing complex dependencies.
Existing solutions
I don't think it's currently possible for numbers of arbitrary size.
Disadvantages
My use case
I'm making an interlocking device for a railway. Each of the paths the train can take through the station (there are 28 variants in my case) has logic dependencies based on the states of each track switch, exit block, occupation of track segments etc.
The current states of each device and segment (non-)occupation can be represented as a single integer, where each bit represents the state of each device. If we for example have 2 switches and 2 track segments with occupancy detection we can represent the state as the sum of:
The dependencies of a train path can also be represented as bitwise masks on this state.
If a path requires switch A to be in position +, switch B in position - and segment X to be free, we can represent the mask as:
MASK = 32 + 4 + 2 = 100000_2 + 000100_2 + 000010_2 = 100110_2 = 37
(in many cases the|
will be preferable over a simple+
)To check if all dependencies of the path are fulfilled we can simply check if:
(STATE & MASK) ^ MASK == 0
or equally in this caseSTATE & MASK == MASK
. This means i need only 2 math blocks per train path, greatly reducing the complexity of the interlocking device.A similar bit-based system can then be used to control the lights that light up on the control panel, the train signals which need to show and the devices of which switching should be locked.
This is the actual values for my dependencies:
Attachments
Issue checklist