Sign in to follow this  

Why does this code work?

This topic is 4536 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

static unsigned int	nMod	= 1;

if(!m_bActive)
	return;

if(m_Object.m_fTime >= m_Object.m_fDelay)
{
	m_Object.m_fTime = 0.0f;
	m_nCurrentFrame += nMod;
	if(m_nCurrentFrame == m_nFrameCount)
	{
		if(!m_bRepeat)
			Activate(false);
		else
			nMod = -1;
	}
	if(m_nCurrentFrame == 0)
		nMod = 1;
}

Why does this code work perfectly? Should "nMod = -1;" set it to some huge number (I don't know it off hand.) It worked perfectly, but IIRC it shouldn't. I was just wondering. Thanks!

Share this post


Link to post
Share on other sites
Because you generate a carry bit.
Or more clear:

lets say you work modolo 10
then -1 mod 10 = 9;

so you can subtract with -1.

lets say you want (5 - 1) mod 10
then you could also write
(5 + 9) = 14
14 mod 10 = 4 which is the same as (5 - 1) = 4 [mod 10]

hope this helps

Share this post


Link to post
Share on other sites

This topic is 4536 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this