modulo

Modulus operation with negatives values - weird thing ??

Can you please tell me how much is (-2) % 5 ? According to my python interpreter is 3, but do you have a wise explanation for this ? I've read that in some languages the result can be machine-dependent, but I'm not sure though. Thanks for your help. ...

Is there an alternative to using % (modulus) in C/C++?

I read somewhere once that the modulus operator is inefficient on small embedded devices such as 8 bit micros without integer division operator. Perhaps someone can confirm this but I thought the difference is 5-10 time slower than with an integer division operation. Is there another way to do this other than keeping a counter variable ...

Algorithm to add two digits to the end of a number to calculate a specific modulus?

So, lets say I have a number 123456. 123456 % 97 = 72. How can I determine what two digits need to be added to the end of 123456 such that the new number % 97 = 1? Note--it must always be two digits. For example, 12345676 % 97 = 1. In this case, I need to add the digits "76" to the end of the number. (This is for IBAN number calcul...

Perform a modulus in a huge number?

I need to do a modulus operation on very large integers. The biggest integer supported by my platform (edit: .NET 2.0) is a 64 bit integer, which aren't big enough for the numbers I'm working with. How can I do a modulus on really big integers, like 12654875632126424875387321657498462167853687516876876? I have a solution that treats t...

What is the fastest way to get the 4 least significant bits in a byte (C++)?

I'm talking about this: If we have the letter 'A' which is 77 in decimal and 4D in Hex. I am looking for the fastest way to get D. I thought about two ways: Given x is a byte. x << 4; x >> 4 x %= 16 Any other ways? Which one is faster? Thanks. ...

How do you do modulo or remainder in Erlang?

I'm brand new to Erlang. How do you do modulo (get the remainder of a division)? It's % in most C-like languages, but that designates a comment in Erlang. Several people answered with rem, which in most cases is fine. But I'm revisiting this because now I need to use negative numbers and rem gives you the remainder of a division, which ...

Why is modulus different in different programming languages?

Perl print 2 % -18; --> -16 Tcl puts [expr {2 % -18}] --> -16 but VBScript wscript.echo 2 mod -18 --> 2 Why the difference? ...

What is the correct way to use the modulus (%) operator in JavaScript?

In JavaScript the % operator seems to behave in a very weird manner. I tried the following: >>> (0 - 11) % 12 -11 Why does it return -11 instead of 1 (as in Python)? I am sure I am doing or expecting something wrong, but the docs don't tell me what. ...

In C#, how do I implement modulus like google calc does?

I have a class which represents a shape. The Shape class has a property called Angle. I want the setter for this property to automatically wrap the value into the range [0,359]. Unfortunately, a simple _Angle = value % 360; only works for positive numbers. In C#, -40 % 360 == -40. Google calc does it the way I want it. The value should ...

How do you calculate div and mod of floating point numbers?

In Perl, the % operator seems to assume integers. For instance: sub foo { my $n1 = shift; my $n2 = shift; print "perl's mod=" . $n1 % $n2, "\n"; my $res = $n1 / $n2; my $t = int($res); print "my div=$t", "\n"; $res = $res - $t; $res = $res * $n2; print "my mod=" . $res . "\n\n"; } foo( 3044.952963...

Python quotient vs remainder

The python 2.6 docs state that x % y is defined as the remainder of x / y (http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex). I am not clear on what is really occurring though, as: for i in range(2, 11): print 1.0 % i prints "1.0" ten times, rather than "0.5, 0.333333, 0.25" etc. as I expected (1/2...

How to get the modulus of a number in XPath/XSLT?

I need to calculate the modulus of a number in XPath, but this won't work: <xsl:if test="(count()%8)"> How would I do it? Looked at the XPath function reference here, but didn't see anything like that. THANKS FOR THE PROMPT ANSWERS! ...

Modulo with long long integers in Objective C

I'm trying use the modulo operator (%) on long longs, and it seems to return 0 if the number is above the range of an unsigned int. Is there an operator or function that I should be using instead, or should I roll my own? -- Update: sorry for the lack of example code before, I was in a hurry, heading out the door -- Here's the code: l...

PHP - Large Interger mod calculation

I need to calculate modulus with large number like : <?php $largenum = 95635000009453274121700; echo $largenum % 97; ?> It's not working... beacause $largenum is too big for an int in PHP. Any idea how to do this ? ...

Computing (a*b) mod c quickly for c=2^N +-1

In 32 bit integer math, basic math operations of add and multiply are computed implicitly mod 2^32, meaning your results will be the lowest order bits of the add or multiply. If you want to compute the result with a different modulus, you certainly could use any number of BigInt classes in different languages. And for values a,b,c < 2^3...

Python-style integer division & modulus in C

In Python and Ruby, signed integer division truncates towards negative infinity, and signed integer modulus has the same sign the second operand: >>> (-41) / 3 -14 >>> (-41) % 3 1 However, in C and Java, signed integer division truncates towards 0, and signed integer modulus has the same sign as the first operand: printf("%d\n", (-41...

Check if a number is divisible by 3

Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a number divisible by 3, otherwise zero. Examples: input "0": (0) output 1 inputs "1,0,0": (4) output 0 inputs "1,1,0,0": (6) out...

Modulo in JavaScript - large number

Hi! I try to calculate with JS' modulo function, but don't get the right result (which should be 1). Here is a hardcoded piece of code. var checkSum = 210501700012345678131468; alert(checkSum % 97); Result: 66 Whats the problem here? Regards, Benedikt ...

C# RSA encryption using modulus and public exponent

I have to implement a digital envelope using AES and RSA, but I am having problems with the .NET implementation of the RSA algorithm. I have managed to encrypt the data (AES) with the random symetric key, but now I have to encrypt the key with RSA. The key is an array of bytes (byte[]) and the public key I have tells me only the modulu...

Handling "Big" Integers in C#

How do I handle big integers in C#? I have a function that will give me the product of divisors: private static int GetDivisorProduct(int N, int product) { for (int i = 1; i < N; i++) { if (N % i == 0) { Console.WriteLine(i.ToString()); product *= i; ...