Archived

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

Bit flags question...

This topic is 5991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

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

Hopefully my logic is fine tuned

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

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;

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633695
• Total Posts
3013376
×