Bit flags question...

Started by
2 comments, last by RegularKid 21 years, 9 months ago
I''ve got 3 16 bit variables. One from the current frame, one from the previous frame, and one that I want to compute based on the previous two variables. I only want bits in this variable to be set if the bit was off in the previous frame and on in the current frame. Otherwise it should be off. The code I came up with works, but I was wondering if there was a better way of doing it because I think the for loop might be overkill. Here is my code:
  
keyJustOn = 0;

// Get the just on bits

for(i = 0; i < 16; i++)
{
    // If the current button was just pressed on this frame

    if((keyDown & (1 << i)) && !(keyDownPrev & (1 << i)))
    {
	// Set the just on bit

	keyJustOn |= (1 << i);
    }
}
  
Any help will be greatly appreciated.
Advertisement

  short last_frame, this_frame, computed;computed = ~last_frame & this_frame;  


Hopefully my logic is fine tuned
Nice! Thanks. I knew I was making it harder than it really was!
Not quite Zipster, if what RegularKids requirements were correct.

He only wants the bit set in the 3rd variable if the bit was set in the 1st, and not in the 2nd.

Your algorithm will set the bit in the 3rd if the bit is set in the 2nd and not in the 1st.

So it should be:

  unsigned short First, Second, Third;...Third = First & ~Second;  

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement