Advertisement
Guest User

BitArray

a guest
Sep 25th, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace BitArray
  8. {
  9.     class BitArray
  10.     {
  11.         public const int BITS_COUNT = 100100;
  12.         private uint[] bitValues = new uint[BITS_COUNT / 32 + 1];
  13.         private static int digits = 0;
  14.         private static string lastPowerOf2 = "1";
  15.         private static int lastPowerOf2int = 0;
  16.         // Indexer declaration
  17.         public int this[int index]
  18.         {
  19.             get
  20.             {
  21.                 if ((index >= 0) && (index < BITS_COUNT))
  22.                 {
  23.                     // Calculate the offset
  24.                     int offset = index / 32;
  25.                     int innerIndex = index % 32;
  26.                     // Check the bit at position index
  27.                     return ((bitValues[offset] >> innerIndex) & 1) == 1 ? 1 : 0;
  28.                 }
  29.                 else
  30.                 {
  31.                     throw new IndexOutOfRangeException(String.Format("Index {0} is invalid!", index));
  32.                 }
  33.             }
  34.  
  35.             set
  36.             {
  37.                 if ((index < 0) || (index > BITS_COUNT - 1))
  38.                 {
  39.                     throw new IndexOutOfRangeException(String.Format("Index {0} is invalid!", index));
  40.                 }
  41.                 if ((value < 0) || (value > 1))
  42.                 {
  43.                     throw new ArgumentException(String.Format("Value {0} is invalid!", value));
  44.                 }
  45.                 // Calculate the offset
  46.                 int offset = index / 32;
  47.                 int innerIndex = index % 32;
  48.                 // Clear the bit at position index
  49.                 bitValues[offset] &= ~((uint)(1 << innerIndex));
  50.                 // Set the bit at position index to value
  51.                 bitValues[offset] |= (uint)(value << innerIndex);
  52.                 if (index > BitArray.digits) BitArray.digits = index;
  53.             }
  54.         }
  55.  
  56.         //methods
  57.         public override string ToString()
  58.         {
  59.             StringBuilder bitsStr = new StringBuilder();
  60.             int offset = 0;
  61.             //bitsStr.Append(string.Format("bits = \noffset {0,5} :", offset));
  62.             bitsStr.Append(new string(' ', 31 - (BitArray.digits % 32)));
  63.             for (int i = BitArray.digits; i >= 0; i--)
  64.             {
  65.                 bitsStr.Append(this[i]);
  66.                 if ((i) % 32 == 0)
  67.                 {
  68.                     offset++;
  69.                     if (offset % 2 == 0) bitsStr.Append("\n");
  70.                     else bitsStr.Append(" ");
  71.                 }
  72.             }
  73.             return bitsStr.ToString();
  74.         }
  75.  
  76.         public string ToBigDecimal()
  77.         {
  78.             StringBuilder bigStr = new StringBuilder();
  79.             string power = "0";
  80.             string sum = "0";
  81.             for (int i = 0; i < BITS_COUNT; i++)
  82.             {
  83.                 if (this[i] == 1)
  84.                 {
  85.                     power = BitArray.BigPower2(i);
  86.                     sum = BitArray.SumTwoHugeNumbers(sum, power);
  87.                 }
  88.             }
  89.             return sum;
  90.         }
  91.  
  92.         public static string SumTwoHugeNumbers(string big1, string big2)
  93.         {
  94.             int len1 = big1.Length;
  95.             int len2 = big2.Length;
  96.  
  97.             if (len1 < len2) big1 = new String('0', len2 - len1) + big1;
  98.             else if (len1 > len2) big2 = new String('0', len1 - len2) + big2;
  99.             char[] ch1 = big1.Reverse().ToArray();
  100.             char[] ch2 = big2.Reverse().ToArray();
  101.  
  102.             int len = ch1.Length;
  103.             char[] result = new char[len];
  104.             int sum = 0, over = 0;
  105.             for (int i = 0; i < len; i++)
  106.             {
  107.                 sum = ch1[i] + ch2[i] + over - 96;
  108.                 over = 0;
  109.                 if (sum > 9)
  110.                 {
  111.                     over = sum / 10;
  112.                     sum %= 10;
  113.                 }
  114.                 result[i] = (char)(sum + 48);
  115.             }
  116.             string answer = new string(result.Reverse().ToArray());
  117.             if (over > 0) return ((char)(over + 48)).ToString() + answer;
  118.             else return answer;
  119.         }
  120.  
  121.         public static string BigPower2(int power)
  122.         {
  123.             if (power < 0) throw new ArgumentOutOfRangeException("Power cannot be negative!");
  124.             //if (power < 31) return ((int)(1 << power)).ToString();
  125.             if (power == 0) return "1";
  126.  
  127.             string tmp = BitArray.lastPowerOf2;
  128.             int i = BitArray.lastPowerOf2int+1;
  129.  
  130.             if (power<BitArray.lastPowerOf2int)
  131.             {
  132.                 i = 0;
  133.                 tmp = "1";
  134.             }
  135.  
  136.             for (; i <= power; i++)
  137.             {
  138.                 tmp = BitArray.SumTwoHugeNumbers(tmp, tmp);
  139.             }
  140.             BitArray.lastPowerOf2int = power;
  141.             BitArray.lastPowerOf2 = tmp;
  142.             return tmp;
  143.         }
  144.     }
  145.  
  146.  
  147.     class Program
  148.     {
  149.         static void Main(string[] args)
  150.         {
  151.             BitArray bits = new BitArray();
  152.             bits[0] = 1;
  153.             bits[1] = 1;
  154.             bits[2] = 0;
  155.             bits[33] = 0;
  156.             bits[10000] = 1;
  157.            
  158.             Console.WriteLine("Binary representation:\n{0}", bits);
  159.             Stopwatch stopwatch = new Stopwatch();
  160.             stopwatch.Start();
  161.             string hudge = bits.ToBigDecimal();
  162.             Console.WriteLine("Hudge integer representation:\n\n{0}\n\ntotal digits {1}\n", hudge, hudge.Length);
  163.             stopwatch.Stop();
  164.             Console.WriteLine("time taken {0} seconds", stopwatch.Elapsed);
  165.            
  166.             //Console.WriteLine(BitArray.SumTwoHugeNumbers("123456789012345", "12131234546785465489798784513229"));
  167.             //Console.WriteLine("2 to {0} is {1}", 100, BitArray.BigPower2(100));
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement