Jump to content
  • Advertisement

Archived

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

RegularKid

Bit flags question...

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

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement

  
short last_frame, this_frame, computed;

computed = ~last_frame & this_frame;


Hopefully my logic is fine tuned

Share this post


Link to 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;

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!