table under for details). modulo 0 is undefined in most systems, although some do define it as . As described by Leijen, However, truncated division satisfies the identity . Notation Some calculators have a function button, and many programming languages have a similar function, expressed as , for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as or . For environments lacking a similar function, any of the three definitions above can be used. Common pitfalls When the result of a modulo operation has the sign of the dividend (truncated definition), it can lead to surprising mistakes. For example, to test if an integer is odd, one might be inclined to test if the remainder by 2 is equal to 1: bool is_odd(int n) { return n % 2 == 1; } But in a language where modulo has the sign of the dividend, that is incorrect, because when (the dividend) is negative and odd, mod 2 returns −1, and the function returns false. One correct alternative is to test that the remainder is not 0 (because remainder 0 is the same regardless of the signs): bool is_odd(int n) { return n % 2 != 0; } Another alternative is to use the fact that for any odd number, the remainder may be either 1 or −1: bool is_odd(int n) { return n % 2 == 1 || n % 2 == -1; } A simpler alternative is to treat the result of n % 2 as if it is a boolean value, where any non-zero value is true: bool is_odd(int n) { return n % 2; } Performance issues Modulo operations might be implemented such that a division with a remainder is calculated each time. For special cases, on some hardware, faster alternatives exist. For example, the modulo of powers of 2 can alternatively be expressed as a bitwise AND operation (assuming is a positive integer, or using a non-truncating definition): x % 2n == x & (2n - 1) Examples: In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations. Compiler optimizations may recognize expressions of the form where is a power of two and automatically implement them as , allowing the programmer to write clearer code without compromising performance. This simple optimization is not possible for languages