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);
}
}
Bit flags question...
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:
Any help will be greatly appreciated.
short last_frame, this_frame, computed;computed = ~last_frame & this_frame;
Hopefully my logic is fine tuned
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:
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;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement