Archived

This topic is now archived and is closed to further replies.

Bit flags question...

Recommended Posts

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.

Share this post

Share on other sites

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

Hopefully my logic is fine tuned

Share this post

Share on other sites
Nice! Thanks. I knew I was making it harder than it really was!

Share this post

Share on other sites
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;

• Forum Statistics

• Total Topics
628378
• Total Posts
2982348

• 10
• 9
• 15
• 24
• 11