Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which logical operations can I use to ignore irrelevant bits when masking?

Context

Let's say that I have a system model which comprises of 8 Boolean variables. Together, they comprise a byte that may expresses the 128 state permutations of my system. Let this byte be stateByte, whereby each bit is one of my variables.

Now, suppose I have some enumerable states, such as:

public enum States
{
    READY     = 0b_0000_0001
    OPERATING = 0b_0100_0000
    FAULT     = 0b_1000_0000
}

If it were that each of the States were discrete, I could easily determine States currentState = (States)stateByte, however my problem is:

My states are only each dependent on a subset of specific bits, not the entire byte. Specifically, there are some bits that are irrelevant depending on the state. To use pseudo notation, I have the scenario below where x notates an irrelevant bit:

public enum States
{
    READY     = 0b_0000_0001 // Exactly this permutation
    OPERATING = 0b_0100_0000 // Exactly this permutation
    FAULT     = 0b_1xxx_xxxx // Only bit 7 need be high to determine a fault
}

Question

How can I use logical, bitwise operators (masking) in order to enumerate states from only relevant bits?

Further Context

For those sticklers for detail who would question why I am trying to do this or why I cannot simply use thresholds, please see below the full state table of the hardware I am integrating:

enter image description here

like image 540
George Kerwood Avatar asked Mar 02 '23 20:03

George Kerwood


1 Answers

If the flags solution is valid then it would be done like this:

    [Flags]
    public enum States
    {
        READY = 0b_0000_0001,
        OPERATING = 0b_0100_0000,
        FAULT = 0b_1000_0000
    }
    static void Main(string[] args)
    {
        var s = (States)5;
        var check = s = States.FAULT | States.OPERATING;
    }
like image 122
Thomas Koelle Avatar answered Apr 07 '23 09:04

Thomas Koelle