Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. /// <summary>
  2. /// A look up array of bit counts for the numbers 0 to 255 inclusive.
  3. /// Declared static for performance.
  4. /// </summary>
  5. public static readonly byte [] BitCountLookupArray = new byte []
  6. {
  7. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  8. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  10. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  11. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  12. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  13. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  15. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  16. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  17. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  18. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  19. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  20. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  21. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  22. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
  23. };
  24.  
  25. /// <summary>
  26. /// Checks to see if this cell lies on a minor diagonal of a power of 2.
  27. /// The condition is met if the binary representation of the product contains
  28. /// at most two 1s.
  29. /// </summary>
  30. public bool IsDiagonalMinorToPowerOfTwo ()
  31. {
  32. int sum = 0;
  33. // this.X and this.Y are BigInteger types.
  34. byte [] bytes = (this.X + this.Y).ToByteArray();
  35.  
  36. for (int i=0; i < bytes.Length; i++)
  37. {
  38. sum += BitCountLookupArray [bytes [i]];
  39.  
  40. if (sum > 2)
  41. {
  42. return (false);
  43. }
  44. }
  45.  
  46. return (true);
  47. }
  48.  
  49. /// <summary>
  50. /// Checks to see if this cell lies on a major diagonal of a power of 2.
  51. /// The binary representation could contain any number of consecutive 1s
  52. /// (could be none) and has to end with at least one 0.
  53. /// ^[0]*[1]*[0]+$ denotes the regular expression of the binary pattern
  54. /// we are looking for.
  55. /// </summary>
  56. public bool IsDiagonalMajorToPowerOfTwo ()
  57. {
  58. byte [] bytes = null;
  59.  
  60. bool moreOnesPossible = true;
  61. System.Numerics.BigInteger number = 0;
  62.  
  63. number = System.Numerics.BigInteger.Abs(this.X - this.Y);
  64.  
  65. if ((number == 0) || (number == 1))
  66. {
  67. // 00000000 && 00000001
  68. return (true);
  69. }
  70. else
  71. {
  72. // The last bit should always be 0.
  73. //if (number.IsEven)
  74. {
  75. bytes = number.ToByteArray();
  76.  
  77. for (int b=0; b < bytes.Length; b++)
  78. {
  79. if (moreOnesPossible)
  80. {
  81. switch (bytes [b])
  82. {
  83. case 001: // 00000001
  84. case 003: // 00000011
  85. case 007: // 00000111
  86. case 015: // 00001111
  87. case 031: // 00011111
  88. case 063: // 00111111
  89. case 127: // 01111111
  90. case 255: // 11111111
  91. {
  92. // So far so good.
  93. // Carry on testing subsequent bytes.
  94.  
  95. break;
  96. }
  97. case 128: // 10000000
  98. case 064: // 01000000
  99. case 032: // 00100000
  100. case 016: // 00010000
  101. case 008: // 00001000
  102. case 004: // 00000100
  103. case 002: // 00000010
  104.  
  105. case 192: // 11000000
  106. case 096: // 01100000
  107. case 048: // 00110000
  108. case 024: // 00011000
  109. case 012: // 00001100
  110. case 006: // 00000110
  111.  
  112. case 224: // 11100000
  113. case 112: // 01110000
  114. case 056: // 00111000
  115. case 028: // 00011100
  116. case 014: // 00001110
  117.  
  118. case 240: // 11110000
  119. case 120: // 01111000
  120. case 060: // 00111100
  121. case 030: // 00011110
  122.  
  123. case 248: // 11111000
  124. case 124: // 01111100
  125. case 062: // 00111110
  126.  
  127. case 252: // 11111100
  128. case 126: // 01111110
  129.  
  130. case 254: // 11111110
  131. {
  132. moreOnesPossible = false;
  133.  
  134. break;
  135. }
  136.  
  137. default:
  138. {
  139. return (false);
  140. }
  141. }
  142. }
  143.  
  144. else
  145. {
  146. if (bytes [b] > 0)
  147. {
  148. return (false);
  149. }
  150. }
  151. }
  152. }
  153. //else
  154. {
  155. //return (false);
  156. }
  157. }
  158.  
  159. return (true);
  160. }
  161.  
  162. ...
  163. bytes = number.ToByteArray();
  164. foreach (byte b in bytes)
  165. {
  166. byte temp = 0;
  167. if (moreOnesPossible)
  168. {
  169. while(temp != 255 && moreOnesPossible)
  170. {
  171. temp = (temp << 1) + 1;
  172. if(b == temp)
  173. continue;
  174.  
  175. byte shift = 0;
  176. do{
  177. if(bytes[b] == (temp << ++shift))
  178. {
  179. moreOnesPossible = false;
  180. break;
  181. }
  182. } while(temp << shift < 128)
  183. }
  184. }
  185. else
  186. {
  187. if (bytes [b] > 0)
  188. return (false);
  189. }
  190. }
  191. ...
  192.  
  193. ...
  194. bytes = number.ToByteArray();
  195. foreach (byte b in bytes)
  196. {
  197. if (moreOnesPossible)
  198. {
  199. if(!validPatterns[b])
  200. {
  201. moreOnesPossible = false;
  202. continue;
  203. }
  204. }
  205. else
  206. {
  207. if (b > 0)
  208. return (false);
  209. }
  210. }
  211. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement