Best way to parse an integer and what exactly is the mod operator

Started by
17 comments, last by dmatter 15 years, 9 months ago
I need a quick way to parse an integer into its separate numbers. I know I can do this the robust way, turn it into a string parse the string and reconvert those back into integers, but I thought I read somewhere that doing 13 % 10, will return the 1 and (13 * 10) % 10 will return the 3 or something like that, but I can't find the post, and I not sure what exactly the '%' operator is doing to get those results?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Advertisement
The modulo operator m%n returns the remainder of the integer division of m by n.
The modulo operator returns the remainder after whole number division:
2 modulo 3 returns 226 modulo 13 returns 09 module 2 returns 1

Integer division discards the remainder, so you can parse a number with a given radix by applying division and modulo:
143 % 10 → 3(143 / 10 → 14) % 10 → 4(14 / 10 → 1) % 10 → 1

That should get you on your way.
thank you you guys.

curious off the tops of your heads, when else could the '%' operator come in handy?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Quote:Original post by freeworld
thank you you guys.

curious off the tops of your heads, when else could the '%' operator come in handy?


Finding out if a number is odd or even is a common use.
The modulus operator can come in handy for hashing key values in a hash table.

It maps a larger range of number to some smaller number of values, between 0 and some N, where N is the size of the hash table. Read up on hash tables for more information on how this works.
Also, if you ever want to 'clamp' an angle to [0-360] or [0-2PI]
540 % 360 = 180fmod (8.343, M_2_PI) = 2.06 // % doesn't work on floating point use fmod()
Quote:Original post by freeworld
thank you you guys.

curious off the tops of your heads, when else could the '%' operator come in handy?


It's used extensively in cryptography, especially public or asymmetric key encryption. You can use the mod operator to set max and min values of a function (rand() for example) cause the maximum value the mod operator will produce for a given value is 1 minus the divisor.
Patrick
Quote:Original post by freeworld
thank you you guys.

curious off the tops of your heads, when else could the '%' operator come in handy?


It's used extensively in cryptography, especially public or asymmetric key encryption. You can use the mod operator to set max and min values of a function (rand() for example) cause the maximum value the mod operator will produce for a given value is 1 minus the divisor.
Patrick
Quote:curious off the tops of your heads, when else could the '%' operator come in handy?


When using rand() (don't forget to seed the random number generator using srand!) to generate a random number, you can use the modulus operator to return a number between 1 and a maximum.

For example, rand() by itself will return a random number governed by whatever RAND_MAX has been defined as (found in stdlib.h). Using rand()%1000 however, would return a random number between 1 and 1000.

This topic is closed to new replies.

Advertisement