Toggle the parent tree of C Programming Modulus Operator %. Learn Software and Hardware Tools Toggle the hierarchy tree under Learn Software and Hardware Tools. Fundamentals of the C Programming Language Toggle the hierarchy tree under Fundamentals of the C Programming Language. C Programming Operators Toggle the hierarchy tree under C Programming Operators. C Programming Modulus Operator % Toggle the hierarchy tree under C Programming Modulus Operator %.
Applications of the Modulus Operator 1. Truncation: x % 2n where n is the desired word width (e.g. 8 for 8-bits: x % 256 )
Returns the value of just the lower n-bits of x 2. Can be used to break apart a number in any base into its individual digits
Example 1 2 3 4 5 6 7 8 9 10
#define MAX_DIGITS 6 long number = 123456 ; int i, radix = 10 ; char digits[MAX_DIGITS]; for (i = 0 ; i < MAX_DIGITS; i++ ) { if (number == 0 ) break ; digits[i] = (char )(number % radix); number /= radix; }
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.