fork download
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4.  
  5. namespace VarIntTest
  6. {
  7. class Program
  8. {
  9. public static void Main (string[] args)
  10. {
  11. Console.WriteLine ("Test VarInt encoding");
  12.  
  13. Console.WriteLine ("{0,6} | = | {1,6} | {2,33}", "Value", "VarInt", "Bits");
  14. Console.WriteLine (new String ('-', 3 * 3 + 6 * 2 + 33 + 1));
  15. for (int n = -12; n < 4; n++) {
  16. uint v = IntToVarInt (n);
  17. Console.WriteLine ("{0,6} | {1} | {2,6} | {3,33}", n, (n == VarIntToInt (v)) ? '=' : '/', v, ShowBits (v));
  18. }
  19.  
  20. Console.WriteLine ("\n{0,6} | = | {1,6} | {2,33}", "Value", "Base-2", "Bits");
  21. Console.WriteLine (new String ('-', 3 * 3 + 6 * 2 + 33 + 1));
  22. for (int n = -12; n < 4; n++) {
  23. uint v = IntToNeg2 (n);
  24. Console.WriteLine ("{0,6} | {1} | {2,6} | {3,33}", n, (n == Neg2ToInt (v)) ? '=' : '/', v, ShowBits (v));
  25. }
  26.  
  27. var sw = new Stopwatch ();
  28.  
  29. for (int n = -100000; n < 100000; n++) {
  30. uint v = IntToVarInt (n);
  31. int i = VarIntToInt (v);
  32. }
  33. sw.Restart ();
  34. for (int n = -100000; n < 100000; n++) {
  35. uint v = IntToVarInt (n);
  36. int i = VarIntToInt (v);
  37. }
  38. sw.Stop ();
  39. Console.WriteLine ("\nInt To VarInt conversion took: {0}", sw.Elapsed);
  40.  
  41. for (int n = -100000; n < 100000; n++) {
  42. uint v = IntToNeg2 (n);
  43. int i = Neg2ToInt (v);
  44. }
  45. sw.Restart ();
  46. for (int n = -100000; n < 100000; n++) {
  47. uint v = IntToNeg2 (n);
  48. int i = Neg2ToInt (v);
  49. }
  50. sw.Stop ();
  51. Console.WriteLine ("Int To Base-2 conversion took: {0}", sw.Elapsed);
  52.  
  53. Console.Write ("Press any key to continue . . . ");
  54. Console.ReadKey (true);
  55. }
  56.  
  57. static StringBuilder sb = new StringBuilder ();
  58.  
  59. public static string ShowBits (int value)
  60. {
  61. return ShowBits ((uint)value);
  62. }
  63.  
  64. public static string ShowBits (uint value)
  65. {
  66. sb.Length = 0;
  67. do {
  68. sb.Append ((value & 1) > 0 ? '1' : '0');
  69. unchecked {
  70. value >>= 1;
  71. }
  72. } while (value != 0);
  73. int j = sb.Length - 1, i = 0;
  74. while (i < j) {
  75. var c = sb[i];
  76. sb[i] = sb[j];
  77. sb[j] = c;
  78. i++;
  79. j--;
  80. }
  81. return sb.ToString ();
  82. }
  83.  
  84. public static uint IntToVarInt (int n)
  85. {
  86. unchecked {
  87. return (uint)((n << 1) ^ (n >> 31));
  88. }
  89. }
  90.  
  91. public static int VarIntToInt (uint v)
  92. {
  93. unchecked {
  94. var n = (int)v;
  95. return (n >> 1) ^ (-(n & 1));
  96. }
  97. }
  98.  
  99. public static uint IntToNeg2 (int n)
  100. {
  101. uint val = 0;
  102. uint shift = 1;
  103. while (n != 0) {
  104. int rem;
  105. n = Math.DivRem (n, -2, out rem);
  106. if (rem < 0) {
  107. ++n;
  108. }
  109. if (rem != 0) {
  110. val |= shift;
  111. }
  112. shift <<= 1;
  113. }
  114. return val;
  115. }
  116.  
  117. public static int Neg2ToInt (uint v)
  118. {
  119. int n = 0;
  120. int shift = 0;
  121. int neg = 1;
  122. while (v > 0) {
  123. int rem = (int)(v & 1);
  124. v >>= 1;
  125. n += (rem << shift) * neg;
  126. ++shift;
  127. neg *= -1;
  128. }
  129. return n;
  130. }
  131. }
  132. }
Success #stdin #stdout 0.1s 29800KB
stdin
Standard input is empty
stdout
Test VarInt encoding
 Value | = | VarInt |                              Bits
-------------------------------------------------------
   -12 | = |     23 |                             10111
   -11 | = |     21 |                             10101
   -10 | = |     19 |                             10011
    -9 | = |     17 |                             10001
    -8 | = |     15 |                              1111
    -7 | = |     13 |                              1101
    -6 | = |     11 |                              1011
    -5 | = |      9 |                              1001
    -4 | = |      7 |                               111
    -3 | = |      5 |                               101
    -2 | = |      3 |                                11
    -1 | = |      1 |                                 1
     0 | = |      0 |                                 0
     1 | = |      2 |                                10
     2 | = |      4 |                               100
     3 | = |      6 |                               110

 Value | = | Base-2 |                              Bits
-------------------------------------------------------
   -12 | = |     52 |                            110100
   -11 | = |     53 |                            110101
   -10 | = |     10 |                              1010
    -9 | = |     11 |                              1011
    -8 | = |      8 |                              1000
    -7 | = |      9 |                              1001
    -6 | = |     14 |                              1110
    -5 | = |     15 |                              1111
    -4 | = |     12 |                              1100
    -3 | = |     13 |                              1101
    -2 | = |      2 |                                10
    -1 | = |      3 |                                11
     0 | = |      0 |                                 0
     1 | = |      1 |                                 1
     2 | = |      6 |                               110
     3 | = |      7 |                               111

Int To VarInt conversion took: 00:00:00.0010748
Int To Base-2 conversion took: 00:00:00.0452521
Press any key to continue . . .