The Labs \ Source Viewer \ SSCLI \ System.Xml \ Bits

  1. //------------------------------------------------------------------------------
  2. // <copyright file="Bits.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Xml
  16. {
  17.     using System;
  18.     using System.Diagnostics;
  19.    
  20.     /// <summary>
  21.     /// Contains static utility methods used to manipulate bits in a word.
  22.     /// </summary>
  23.     static internal class Bits
  24.     {
  25.         private static readonly uint MASK_0101010101010101 = 1431655765;
  26.         private static readonly uint MASK_0011001100110011 = 858993459;
  27.         private static readonly uint MASK_0000111100001111 = 252645135;
  28.         private static readonly uint MASK_0000000011111111 = 16711935;
  29.         private static readonly uint MASK_1111111111111111 = 65535;
  30.        
  31.         /// <summary>
  32.         /// Returns the number of 1 bits in an unsigned integer. Counts bits by divide-and-conquer method,
  33.         /// first computing 16 2-bit counts, then 8 4-bit counts, then 4 8-bit counts, then 2 16-bit counts,
  34.         /// and finally 1 32-bit count.
  35.         /// </summary>
  36.         public static int Count(uint num)
  37.         {
  38.             num = (num & MASK_0101010101010101) + ((num >> 1) & MASK_0101010101010101);
  39.             num = (num & MASK_0011001100110011) + ((num >> 2) & MASK_0011001100110011);
  40.             num = (num & MASK_0000111100001111) + ((num >> 4) & MASK_0000111100001111);
  41.             num = (num & MASK_0000000011111111) + ((num >> 8) & MASK_0000000011111111);
  42.             num = (num & MASK_1111111111111111) + (num >> 16);
  43.            
  44.             return (int)num;
  45.         }
  46.        
  47.         /// <summary>
  48.         /// Returns true if the unsigned integer has exactly one bit set.
  49.         /// </summary>
  50.         public static bool ExactlyOne(uint num)
  51.         {
  52.             return num != 0 && (num & (num - 1)) == 0;
  53.         }
  54.        
  55.         /// <summary>
  56.         /// Returns true if the unsigned integer has more than one bit set.
  57.         /// </summary>
  58.         public static bool MoreThanOne(uint num)
  59.         {
  60.             return (num & (num - 1)) != 0;
  61.         }
  62.        
  63.         /// <summary>
  64.         /// Clear the least significant bit that is set and return the result.
  65.         /// </summary>
  66.         public static uint ClearLeast(uint num)
  67.         {
  68.             return num & (num - 1);
  69.         }
  70.        
  71.         /// <summary>
  72.         /// Compute the 1-based position of the least sigificant bit that is set, and return it (return 0 if no bits are set).
  73.         /// (e.g. 0x1001100 will return 3, since the 3rd bit is set).
  74.         /// </summary>
  75.         public static int LeastPosition(uint num)
  76.         {
  77.             if (num == 0)
  78.                 return 0;
  79.             return Count(num ^ (num - 1));
  80.         }
  81.     }
  82. }

Developer Fusion