# Why does this code work?

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

## 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.

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633312
• Total Posts
3011312
• ### Who's Online (See full list)

There are no registered users currently online

×