Bitwise operation for state machine

Hello All,
I am writing basic state machine which maintains 8 different states and there is posibility that system may be in multiple states at a time (Except for state1 to state3. menas only once state can be active at a time from state1 to state3).
I have declared enum like one below

enum
{
   state1 1
   state2 1 << 1
   state3 1 << 2
   state4 1 << 4
    ....
    ....
}

Now my requirement is that state1 to state3 can not be active at a given instance.
Example if machine is in state1 then; state variabale would be -->

 xxxx x001 

... then when system changes its state to to state3 ... state variable should become

xxxx x100.

.

Note :- "x'" indicates other state variable which should not get affected because of above operations.

Thank you very much in advance.

Regards,
Anand Shah

So you have a variable (e.g. state ) that presumably should always have no more than one of the three low order bits set. (Or, maybe you mean it should always have exactly one of the three low order bits set. Your specification is ambiguous as to which of these you mean.)

How will you tell your machine to change state?
Will you call a function to change the state? If so, why is that function not just an assignment to state ? Is it that you want to verify that the requested new state has no more than one (or exactly one) of the three low order bits set?

Maybe you want a function that takes two arguments, states to add to state and states to remove from state .

What language are you using? (I could guess C, but it isn't the only language that includes enums.)

What have you tried so far?

Dear Don Cragun,
Thank you very much for your reply.
I am using C macro to accomplish this. I can not pass states to remove from

state

as I am not aware of previous state. What I want is their should always have no more than one of the three low order bits set.
Example :- If I am in

state1

currently then my state variable would be

 xxxx x001

. Now If I want to set state to

state3

then my state variable should become

 xxxx x100.
                      Thank you.

This makes no sense. If your program isn't aware of your current state (previous state when you want to change to a new set of states), then setting the new state is just a statement like:

state = state1;
    or
state = state3 | state4 | state7;

If you can't restrain yourself from writing code like:

state = state1 | state2;

and you aren't going to set up a function to perform verification of state changes, I have no idea what you are trying to do. And, doing this, there are never any unknown (or unchanged states); you replace (rather than update) states every time to assign a new value to state . So you won't have xxxx x100 , you'll have 0000 0001 , 0100 1100 , or (the undesired) 0000 0011 , respectively, for the three samples above; but never any x s.

I must be missing something in your description of what you're trying to do.

Dear ,
I am really sorry for my idiot reply in last post.
Yes I store the last state which I am exposing to my application through another macro.
Thus there are basically two macros each for saving the state and getting the current state.

OK. So, show us the macros! And, tell us what you want each of those macros to do that they are not doing now.

Here are the macros :-

#define INA_SAVE_APP_STATE(STATE)  appState = (appState | STATE); 
#define INA_GET_APP_STATE() appState

The problem with above macro is that when i switch from

state1

to

state3

; state variable becomes

xxxx x101

that means both

state1

and

state3

are active where as what I want is when I switch from

state1

to

state3

state variable should be

xxxx x100

meaning only

state3

is active

If I'm understanding you correctly, INA_SAVE_APP_STATE(STATE) should clear state1, state2, and state3 from appState and then or in all bits present in the operand. So, states 4 through 8 can be set at any time and once set cannot be cleared. And whatever is set in the low order three bits of the operand will replace whatever was in those three bits before. If this is what you want, try:

#define INA_SAVE_APP_STATE(STATE)  appState = ((appState & !(state1 | state2 | state3)) | STATE);

To be on the safe side, you might want to AND the STATE operand with 0xF8 as well, and check that in STATE only one bit is set...

Agreed. I'm just not sure that we have a true specification of what this macro is supposed to do yet. I've worked on a lot of finite state machines, but the state changes described for this project still seem unusual to me. There seems to be a state0, but instead of having a bit identifying state0; it is identified by being not in state1, state2, or state3.

I was expecting to hear that my suggested change is a step in the right direction, but still isn't doing some things right. Should INA_SAVE_APP_STATE(state4) really clear state1 , state2 , and state3 from appState ? Shouldn't there be some way to clear state4 through state8 ? Shouldn't there be a macro to initialize the state? (Maybe the header always defines appState to be an extern int (or char); but we haven't seen any evidence of that yet, and these macros won't behave as desired if appState is an uninitialized variable allocated on the stack.) There is no defined error mechanism. (What should happen if one calls INA_SAVE_APP_STATE(state1 | state2) ?) Et cetera.

I'm still hoping for an actual set of specifications of the allowed state transitions, what happens when an illegal state is passed in, etc.