Advertisement Jump to content
Sign in to follow this  

Why does this code work?

This topic is 4932 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_Object.m_fTime >= m_Object.m_fDelay)
	m_Object.m_fTime = 0.0f;
	m_nCurrentFrame += nMod;
	if(m_nCurrentFrame == m_nFrameCount)
			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
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!