Why does this code work?

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 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 on other sites
Thank you very much. That had me confused all night.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627667
• Total Posts
2978533

• 9
• 10
• 10
• 12
• 22