how to find modulus of a number in c# .net?
With the modulus operator %:
int x = 4545;
int mod = x % 16;
Via the '%' operator.
e.g. int i = 10 % 3; (result: i is 1)
Use this site
http://www.willasrari.com/blog/use-c-modulus-operator-to-determine-even-or-odd-numbers/000166.aspx
Basically the % opearator: x%y
Related
Is there a mod operator in C# for RSA algorithm? I've been using % as I thought this could be used as mod, but the answers I get for for c and m are not correct, so I've realised % doesn't work.
double e = 13;
double d; //decryption
double de = 7;
d = ((de * euiler) + 1) / e;
double message = 25;
double c = Pow(message, e) % n;
double m = Pow(c, d) % n;
The confusion lies in double Type.
MSDN:
The modulus operator (%) computes the remainder after dividing its first operand by its second. All numeric types have predefined modulus
operators.
Note the round-off errors associated with the double type.
the % is a remainder. You might want to make a static function that uses the % to make a modulo operation.
I've found method which implements Adler32 algorithm in C# and I would like to use it, but I do not understand part of the code:
Can someone explain me:
1) why bit operators are used when sum1, and sum2 are initialized
2) why sum2 is shifted ?
Adler32 on wiki https://en.wikipedia.org/wiki/Adler-32
& operator explanation:
(Binary AND Operator copies a bit to the result if it exists in both operands)
private bool MakeForBuffer(byte[] bytesBuff, uint adlerCheckSum)
{
if (Object.Equals(bytesBuff, null))
{
checksumValue = 0;
return false;
}
int nSize = bytesBuff.GetLength(0);
if (nSize == 0)
{
checksumValue = 0;
return false;
}
uint sum1 = adlerCheckSum & 0xFFFF; // 1) why bit operator is used?
uint sum2 = (adlerCheckSum >> 16) & 0xFFFF; // 2) why bit operator is used? , why is it shifted?
for (int i = 0; i < nSize; i++)
{
sum1 = (sum1 + bytesBuff[i]) % adlerBase;
sum2 = (sum1 + sum2) % adlerBase;
}
checksumValue = (sum2 << 16) + sum1;
return true;
}
1) why bit operator is used?
& 0xFFFF sets the two high bytes of the checksum to 0, so sum1 is simply the lower 16 bits of the checksum.
2) why bit operator is used? , why is it shifted?
adlerCheckSum >> 16 shifts the 16 higher bytes down to the lower 16 bytes, & 0xFFFF does the same as in the first step - it sets the 16 high bits to 0.
Example
adlerChecksum = 0x12345678
adlerChecksum & 0xFFFF = 0x00005678
adlerChecksum >> 16 = 0x????1234
(it should be 0x00001234 in C# but other languages / compilers "wrap the bits around" and you would get 0x56781234)
(adlerChecksum >> 16) & 0xFFFF = 0x00001234 now you can be sure it's 0x1234, this step is just a precaution that's probably unnecessary in C#.
adlerChecksum = 0x12345678
sum1 = 0x00005678
sum2 = 0x00001234
Those two operations combined simply split the UInt32 checksum into two UInt16.
From the adler32 Tag-Wiki:
Adler-32 is a fast checksum algorithm used in zlib to verify the results of decompression. It is composed of two sums modulo 65521. Start with s1 = 1 and s2 = 0, then for each byte x, s1 = s1 + x, s2 = s2 + s1. The two sums are combined into a 32-bit value with s1 in the low 16 bits and s2 in the high 16 bits.
I need to compute r = gkmod p where both g and k could be large integer, I mean they could be 64 bit integer. Is there any way?
You can take a look at the BigInteger.ModPow method. BigInteger can represent arbitrarily large integer values.
The ModPow method evaluates the following expression:
(baseValue ^ exponent) Mod modulus
Example from MSDN:
BigInteger number = 10;
int exponent = 3;
BigInteger modulus = 30;
Console.WriteLine("({0}^{1}) Mod {2} = {3}",
number, exponent, modulus,
BigInteger.ModPow(number, exponent, modulus));
//Result: (10^3) Mod 30 = 10
If you don't want to use that one but instead implement the operation yourself, there are certain techniques that can be used to do it efficiently.
In case you would prefer to stay with integers you can also implement your own ModularPower method:
public static int ModularPower(int baseVal, int expVal, int modVal)
{
int initialVal = 1;
for (int i = 0; i < expVal; ++i)
{
initialVal = (initialVal * baseVal) % modVal;
}
return initialVal;
}
Details of the algorithm and a pseudo code on Wikipedia.
Sorry for a possible repeat question the % symbol doesn't coincide with searchability.
What does % mean? I can't seem to stick this one down.
Example:
rotation = value % MathHelper.TwoPi;
is a specific instance.
But I have found code that uses % more often. Modulus I 'think' it is called, but I am not positive.
Previous Post:
With well thought out awnser
% Operator (C# Reference)
The % operator computes the remainder after dividing its first operand
by its second. All numeric types have predefined remainder operators.
See MSDN:
C# Operators
% Operator (C# Reference)
It is the modulus operator. It computes the remainder after dividing its first operand by its second, e.g.
5 % 2 = 1
6 % 2 = 0
5 % 3 = 2.
in C# it means modulus, which is basically a remainder of
example:
int remainder = 10 % 3 //remainder is 1
It is the modulus operator. It returns the remainder of an integer.
int remainder = 2 % 1; // (remainder variable is assigned to 0)
int remainder2 = 3 % 2; // (remainder variable is assigned to 1)
Yes it' the modulus.
http://en.wikipedia.org/wiki/Modulus_(algebraic_number_theory)
Let's say that
x / y = z,
x, y, z being integers.
There is no guarantee that
z * y = x, because the "/" operator rounds down.
So we must add a remainder to our equation:
z * y = x + r.
z * y = x + r
z * (-y) = - (z * y) = -(x + r) = -x - r
This means that the result of the "%" operator can be negative, which means that the "%" or remainder operator differs from the modulo relation, because the result is not guaranteed to be canonical.
pls, I HAVE A NUMBER say 9 and i want to find how to create a program to check if a number b is maybe 21(ie 9+12) or 33(ie 9 + 24) or 45(9 + 36) and so on. Can i get it in C# or SQL
With the clarification, it looks like you want to find whether there is an integer x for which (in math terms, not code)
b = 9 + 12x
is true; so you want to know whether b-9 is some multiple of 12; which is easy:
bool isMatch = ((b - 9) % 12) == 0;
and if you want to know which x:
int x = (b - 9) / 12;
It's not entirely clear from your question, but I think you're looking for the modulo operator. 21 % 12 = 9, 33 % 12 = 9, 45 % 12 = 9.
In C# and SQL this is just %, and it is used like an arithmetic operator (+, -, etc)
I think you've got three variables and than the solution will be like this:
var a = 9;
var b = 12;
var c = 21;
var isInRange = IsInRange(c, a, b);
private bool IsInRange(int input, int offset, int multiple){
return ((input - offset) % multiple) == 0;
}
Subtract your original number (in this case 9) from the number B, and then see if B % 12 is different from zero.