Skip to content

Instantly share code, notes, and snippets.

@dori4n
Created March 30, 2015 21:38
Show Gist options
  • Save dori4n/f866dde1b7d45d58ce89 to your computer and use it in GitHub Desktop.
Save dori4n/f866dde1b7d45d58ce89 to your computer and use it in GitHub Desktop.
System.Numerics glitch?
using System;
using System.Numerics;
using System.Security.Cryptography;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();
byte[] data = new byte[4];
rnd.GetBytes(data);
BigInteger int1 = new BigInteger(data);
rnd.GetBytes(data);
Console.WriteLine();
BigInteger int2 = new BigInteger(data);
int1.WriteToConsole();
Console.WriteLine();
int2.WriteToConsole();
Console.WriteLine();
Console.WriteLine("--------");
BigInteger int3 = int1 ^ int2; // works dandy
int3.WriteToConsole();
Console.WriteLine();
Console.WriteLine();
int3.WriteToConsole();
Console.WriteLine(" ==");
(int3 << 1).WriteToConsole(); // kinda screwy
Console.WriteLine(" <<");
(int3 >> 1).WriteToConsole(); // works more often, but still screwy
Console.WriteLine(" >>");
Console.ReadKey();
}
}
public static class Extensions
{
public static void WriteToConsole(this BigInteger bigNumber)
{
string numString = bigNumber.ToString("x");
for (int i = 0; i < 8; i++)
{
if (numString.Length >= 8) //just to make sure I'm not going insane...
{
break;
}
else
{
numString = string.Concat("0", numString);
}
}
Console.Write(numString);
Console.Write(" ");
foreach (byte b in (bigNumber << 1).ToByteArray())
{
Console.Write(Convert.ToString(b, 2).PadLeft(8, '0'));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment