using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.Globalization;
using System.Diagnostics;
namespace ConsoleApplication11
{
class Program
{
static int[] HowMuch1 = new int[] { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,
2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,
4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,
3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};
static void Main(string[] args)
{
Stopwatch SW = new Stopwatch();
SW.Start();
BigInteger B = new BigInteger(234235436457658);
BigInteger A = (BigInteger.One << 3999) + B;
Tuple<int, int> Sim = MatchDegree(A, B);
SW.Stop();
Console.WriteLine("相同数" + Sim.Item1);
Console.WriteLine("总数" + B.ToByteArray().Length * 8);
Console.WriteLine("偏移量" + Sim.Item2);
Console.WriteLine("毫秒数" + SW.ElapsedMilliseconds);
Console.Read();
}
public static Tuple<int, int> MatchDegree(BigInteger A, BigInteger B)
{
Tuple<int, int> max = new Tuple<int, int>(0, 0);
int a = A.ToByteArray().Length;
B = B << a;
int offset = 0;
while (B > BigInteger.One)
{
B >>= 1;
offset++;
int count = MatchDegree(~(A ^ B));
if (count > max.Item1) { max = new Tuple<int, int>(count, offset); }
}
return max;
}
public static int MatchDegree(BigInteger A)
{
byte[] b = A.ToByteArray();
int count = 0;
for (int i = 0; i < b.Length; i++)
{
count += HowMuch1[b[i]];
}
return count;
}
//判断一个ulong数字里有几个1
public static int MatchDegree(ulong u) { u = (u & 0x5555555555555555) + ((u >> 1) & 0x5555555555555555); u = (u & 0x3333333333333333) + ((u >> 2) & 0x3333333333333333); u = (u & 0x0F0F0F0F0F0F0F0F) + ((u >> 4) & 0x0F0F0F0F0F0F0F0F); u = (u & 0x00FF00FF00FF00FF) + ((u >> 8) & 0x00FF00FF00FF00FF); u = (u & 0x0000FFFF0000FFFF) + ((u >> 16) & 0x0000FFFF0000FFFF); u = (u & 0x00000000FFFFFFFF) + ((u >> 32) & 0x00000000FFFFFFFF); return Convert.ToInt32(u); }
private static string DisplayInBinary(BigInteger number)
{
byte[] bytes = number.ToByteArray();
string binaryString = string.Empty;
foreach (byte byteValue in bytes)
{
string byteString = Convert.ToString(byteValue, 2).Trim();
binaryString += byteString.Insert(0, new string('0', 8 - byteString.Length));
}
return binaryString;
}
}
}