Jump to content
  • Advertisement
Sign in to follow this  
Prune

Divisible by 8

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

Advertisement
For divisible by 2, you check bit 0.
For divisible by 4, you check bits 0, 1.
For divisible by 8, you check bits 0, 1, 2.

So, yes.

Share this post


Link to post
Share on other sites
It should also work for negative numbers if my understanding is correct.

0 0 0 0 1 0 0 0 (8)
1 1 1 1 0 1 1 1
1 1 1 1 1 0 0 0 (-8)

0 0 0 1 1 0 0 0 (24)
1 1 1 0 0 1 1 1
1 1 1 0 1 0 0 0 (-24)


EDIT: opps apparently I cant add 1 :)

[Edited by - Fire Lancer on May 21, 2009 9:03:55 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Prune
If val is unsigned int, can I just check val & 0x7 ?

Yes.

inline bool divisible8(unsigned val)
{
return (val & 7) == 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Fire Lancer
0 0 0 1 1 0 0 0 (24)
1 1 1 0 0 1 1 1
1 1 1 0 1 0 0 0 (-24)

Fixed =P (otherwise it was -8)

Though yeah. In fact, if you use % 8 instead and the compiler has optimizations enabled, it will most likely do the replacement. This is also true for multiplication and division by powers of two (when using integers, of course), where they get replaced by shifting to left and right whatever amount is needed to do the equivalent, respectively.

EDIT: misquoting (missing tag >_<')

EDIT 2: typo. Is it obvious that I just woke up >_>'

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net 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!