Twos Complement On A Double - c#

How do I perform a twos complement on a double and return a double?

If you are trying to do the two's complement of the internal bit representation of the double, you can use the BitConverter class.
Something like:
double x = 12345.6;
Int64 bits = BitConverter.DoubleToInt64Bits(x);
bits = ~bits + 1;
x = BitConverter.Int64BitsToDouble(bits);
I'm not sure why you would want to do this, though...

You might need to cast to a long and then do the twos complement and cast back:
double x = 1245.1;
long l = (long)x;
l=~l; l++; /* complement followed by + 1 */
x = (double)l;
I didn't test this, but hopefully it gets you on the right track.
Edit: Since you cannot cast from double to long with bit representation then you might need to do something like:
double x = 1234.5;
ulong l;
unsigned char * d = (unsigned char *) &x;
l = (ulong)(*d);
l=~l; l++;
d = (unsigned char *) &l;
x = (double)(*d);
Again untested...

Related

How can I have a double value using Math.DivRem()?

I can't show the double values in C#
Math.DivRem(double money, int number, out int remain);
Math.DivRem Method is only compatible with Int64 and Int32, which means you can't use double, float or any numeric data type with decimals.
Depending on what you are looking for, this is two other methods of getting the quotient and remainder of your doubles:
double a = 20.1;
double b = 4.93;
double remainder = a % b; // Returns 0.380...
double quotient = a / b; // Returns 4,077...
If you want to make your remainder or quotient to an int, just use
int result = Convert.ToInt32(quotient); // Returns 4
Or to make it more efficient
int result = ConvertToInt32(a / b); // Returns 4
EDIT
One workaround to use Math.DivRem with "doubles", are by multiplying both sides with a constant, for example 100 000. After getting the remainder, divide it with the constant.
double money = 5.515;
int number = 2;
int constant = 100000;
Math.DivRem(Convert.ToInt32(money * constant), number * constant, out int remain);
// Divides 551500 with 200000
// remain = 151500
// For double
double d = (double) remain / (double) constant; // Return 1,515
// For int
remain /= constant; // Return 1
DOUBLE EDIT
Use this instead of Math.DivRem() when dealing with doubles and ints combined.
double money = 12.15;
int number = 5;
double remainder = money % number; // Return 2,15...
int remainder = (int)(money % number); // Return 2

Mod operator in C#

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.

How to encrypt an ASCII character with textbook RSA in C#?

I have a problem with calculate of ASCII value with exponent and modulus.
I want calculate ASCII value of "K" with RSA algorithm.
K in ascii value is 75
c = m^e mod n
= 75^41 mod 689
= 316
Then how to make it into source code in C#? I got error "cannot implicity convert type".
this my source code
int n = 689;
int e = 41;
int d = 137;
string value = "K";
for (int i = 0; i < value.Length; i++)
{
int c = (int)i;
c = Math.Pow(i,e);
}
Console.ReadLine();
Since 75^41 will overflow if cast to an int you have to do a little math trick. A*B mod N is equivalent to (A mod N) * (B mod N) mod N, so you just do the multiplication in a loop, taking the remainder each time:
public static int PowModN(int a, int b, int n)
{
a = a % n;
int c = 1;
for(int i=1; i <= b; i++)
c = (c*a % n);
return c;
}
and change your loop to:
for (int i = 0; i < value.Length; i++)
{
int c = (int)i;
c = PowModN(i,e,n);
}
string value = "K";
// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
Once you get the array out put you can set it to a variable and do whatever math you need to do.
The output of Math.Pow is a double, and takes two floats as arguments. At the very least, cast the output of Math.Pow(i,e) to be an int, like so:
c = (int)Math.Pow(i,e)
This is one of the worst things about C#, imo. Not sure why it doesn't innately support integer exponentiation.
What type is i? It might need to be casted to doubles as well.

Power a large number with a large number then mode the result

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.

why this would result in long integer overflow

I checked the document that long= int64 has range more than 900,000,000,000,000
Here is my code:
int r = 99;
long test1 = r*r*r*r*r;
at runtime it gives me 919,965,907 instead of the correct 9,509,900,499.
another test
long test2 = 99*99*99*99*99;
It refuses to compile, saying integer overflow.
But if i do this
long test3 = 10100200300;
This works fine.
The problem is that the literal "99" is being treated as an int. If you add "L" it will treat it as a long. To fix your compilation problem:
long test2 = 99L * 99L * 99L * 99L * 99L;
And to fix the "incorrect result" caused by integer overflow:
long r = 99;
long test1 = r * r * r * r * r;
The key point is that the expression to the right of the "=" is evaluated before the assignment to long r is done.
There are other literal suffixes you might be interested in:
Type Suffix Example
uint U or u 100U
long L or l 100L
ulong UL or ul 100UL
float F or f 123.45F
decimal M or m 123.45M
#m.edmonson, regarding your question about why it comes out to 919965907. What's happening, is that the value is "wrapping" around int.MaxValue. You can see this with a little test program:
int i = 99; // 99
i *= 99; // 9801
i *= 99; // 970299
i *= 99; // 96059601
i *= 99; // 919965907 should be 9509900499 but comes out to 919965907
// which is (9509900499 % int.MaxValue)
long k = 9509900499 % int.MaxValue;
What is meant by "wrapping around"? When you exceed int.MaxValue by 1, the value "goes back" to int.MinValue.
int j = int.MaxValue;
j++;
bool isNowMinValue = (j == int.MinValue); // true, the value has "wrapped around"
This is a bit simplistic; if you search for "integer overflow" you will get a better explanation. It's worth understanding how integers (and other numeric types) are represented with 32 bits:
http://en.wikipedia.org/wiki/Signed_number_representations
It's using integer multiplication :
long r = 99;
long test1 = r*r*r*r*r;
As the other have said, but:
long test2 = 99L * 99 * 99 * 99 * 99;
This will give you the correct result with less L around :-)
This happens because the first 99L is a long, so all the multiplications are done in the long "field" and all the other integers are upcasted to long before the multiplication (clearly the multiplication is always between 2 numbers and it's from left to right, so it's like (((99L * 99) * 99) * 99) * 99 and each "partial" result is a long and causes the next operand to be converted to long.)
Your second test fails because each 99 is an integer; replace it with the following and it compiles.
long test2 = 99L * 99L * 99L * 99L * 99L;
See the MSDN Long Documentation for details.
The compiler is looking at 99 as integers, even though the final result will be long.
This will work.
long test2 = 99L*99L*99L*99L*99L;

Categories

Resources