Bit Helper

//GNU Library General Public License (LGPL)
//http://dac.codeplex.com/license
using System;
using System.Security;

namespace RaisingStudio.Collections.Generic
{
internal class BitHelper
{
private const byte IntSize = 0x20;
private int[] m_array;
private unsafe int* m_arrayPtr;
private int m_length;
private const byte MarkedBitFlag = 1;
private bool useStackAlloc;

#if (PocketPC || Smartphone)
#else
[SecurityCritical]
#endif
internal unsafe BitHelper(int* bitArrayPtr, int length)
{
this.m_arrayPtr = bitArrayPtr;
this.m_length = length;
this.useStackAlloc = true;
}

internal BitHelper(int[] bitArray, int length)
{
this.m_array = bitArray;
this.m_length = length;
}

[SecurityCritical]
internal unsafe bool IsMarked(int bitPosition)
{
if (this.useStackAlloc)
{
int num = bitPosition / 0x20;
return (((num < this.m_length) && (num >= 0)) && ((this.m_arrayPtr[num] & (((int)1) << (bitPosition % 0x20))) != 0)); } int index = bitPosition / 0x20; return (((index < this.m_length) && (index >= 0)) && ((this.m_array[index] & (((int)1) << (bitPosition % 0x20))) != 0)); } [SecurityCritical] internal unsafe void MarkBit(int bitPosition) { if (this.useStackAlloc) { int num = bitPosition / 0x20; if ((num < this.m_length) && (num >= 0))
{
int* numPtr1 = this.m_arrayPtr + num;
numPtr1[0] |= ((int)1) << (bitPosition % 0x20); } } else { int index = bitPosition / 0x20; if ((index < this.m_length) && (index >= 0))
{
this.m_array[index] |= ((int)1) << (bitPosition % 0x20); } } } internal static int ToIntArrayLength(int n) { if (n <= 0) { return 0; } return (((n - 1) / 0x20) + 1); } } } [/csharp]

Count the number of bit

//http://extensionlibrary.codeplex.com/
//The MIT License (MIT)
using System;
using System.Collections.Generic;
using System.Text;

namespace ExtensionLibrary.Tools
{
public static class BitOperator
{
#region Count the number of bit one

public static int GetCountOfBitOne(sbyte x)
{
int result = 0;
while (x != 0)
{
result++;
x &= (sbyte)(x – 1);
}
return result;
}

public static int GetCountOfBitOne(short x)
{
int result = 0;
while (x != 0)
{
result++;
x &= (short)(x – 1);
}
return result;
}

public static int GetCountOfBitOne(int x)
{
int result = 0;
while (x != 0)
{
result++;
x &= (x – 1);
}
return result;
}

public static int GetCountOfBitOne(long x)
{
int result = 0;
while (x!=0)
{
result++;
x &= (x – 1);
}
return result;
}

public static int GetCountOfBitOne(byte x)
{
int result = 0;
while (x != 0)
{
result++;
x &= (byte)(x – 1);
}
return result;
}

public static int GetCountOfBitOne(ushort x)
{
int result = 0;
while (x != 0)
{
result++;
x &= (ushort)(x – 1);
}
return result;
}

public static int GetCountOfBitOne(uint x)
{
int result = 0;
while (x != 0)
{
result++;
x &= (x – 1);
}
return result;
}

public static int GetCountOfBitOne(ulong x)
{
int result = 0;
while (x != 0)
{
result++;
x &= (x – 1);
}
return result;
}

#endregion

#region Count the number of bit zero

public static int GetCountOfBitZero(sbyte x)
{
return GetCountOfBitOne(~x);
}

public static int GetCountOfBitZero(short x)
{
return GetCountOfBitOne(~x);
}

public static int GetCountOfBitZero(int x)
{
return GetCountOfBitOne(~x);
}

public static int GetCountOfBitZero(long x)
{
return GetCountOfBitOne(~x);
}

public static int GetCountOfBitZero(byte x)
{
return GetCountOfBitOne(~x);
}

public static int GetCountOfBitZero(ushort x)
{
return GetCountOfBitOne(~x);
}

public static int GetCountOfBitZero(uint x)
{
return GetCountOfBitOne(~x);
}

public static int GetCountOfBitZero(ulong x)
{
return GetCountOfBitOne(~x);
}

#endregion

#region Get number of leading zero

public static int GetNumberOfLeadingZero(sbyte x)
{
int number = 8;

sbyte y = (sbyte)(x >> 4);
if (y != 0)
{
number -= 4;
x = y;
}

y = (sbyte)(x >> 2);
if (y != 0)
{
number -= 2;
x = y;
}

y = (sbyte)(x >> 1);
if (y != 0)
{
return number – 2;
}

return number – x;
}

public static int GetNumberOfLeadingZero(short x)
{
int number = 16;

short y = (short)(x >> 8);
if (y != 0)
{
number -= 8;
x = y;
}

y = (short)(x >> 4);
if (y != 0)
{
number -= 4;
x = y;
}

y = (short)(x >> 2);
if (y != 0)
{
number -= 2;
x = y;
}

y = (short)(x >> 1);
if (y != 0)
{
return number – 2;
}

return number – x;
}

public static int GetNumberOfLeadingZero(int x)
{
int number = 32;

int y = (x >> 16);
if (y != 0)
{
number -= 16;
x = y;
}

y = (x >> 8);
if (y != 0)
{
number -= 8;
x = y;
}

y = (x >> 4);
if (y != 0)
{
number -= 4;
x = y;
}

y = (x >> 2);
if (y != 0)
{
number -= 2;
x = y;
}

y = (x >> 1);
if (y != 0)
{
return number – 2;
}

return number – x;
}

public static int GetNumberOfLeadingZero(long x)
{
int number = 64;

long y = (x >> 32);
if (y != 0)
{
number -= 32;
x = y;
}

y = (x >> 16);
if (y != 0)
{
number -= 16;
x = y;
}

y = (x >> 8);
if (y != 0)
{
number -= 8;
x = y;
}

y = (x >> 4);
if (y != 0)
{
number -= 4;
x = y;
}

y = (x >> 2);
if (y != 0)
{
number -= 2;
x = y;
}

y = (x >> 1);
if (y != 0)
{
return number – 2;
}

return (int)(number – x);
}

public static int GetNumberOfLeadingZero(byte x)
{
int number = 8;

byte y = (byte)(x >> 4);
if (y != 0)
{
number -= 4;
x = y;
}

y = (byte)(x >> 2);
if (y != 0)
{
number -= 2;
x = y;
}

y = (byte)(x >> 1);
if (y != 0)
{
return number – 2;
}

return number – x;
}

public static int GetNumberOfLeadingZero(ushort x)
{
int number = 16;

ushort y = (ushort)(x >> 8);
if (y != 0)
{
number -= 8;
x = y;
}

y = (ushort)(x >> 4);
if (y != 0)
{
number -= 4;
x = y;
}

y = (ushort)(x >> 2);
if (y != 0)
{
number -= 2;
x = y;
}

y = (ushort)(x >> 1);
if (y != 0)
{
return number – 2;
}

return number – x;
}

public static int GetNumberOfLeadingZero(uint x)
{
int number = 32;

uint y = (x >> 16);
if (y != 0)
{
number -= 16;
x = y;
}

y = (x >> 8);
if (y != 0)
{
number -= 8;
x = y;
}

y = (x >> 4);
if (y != 0)
{
number -= 4;
x = y;
}

y = (x >> 2);
if (y != 0)
{
number -= 2;
x = y;
}

y = (x >> 1);
if (y != 0)
{
return number – 2;
}

return (int)(number – x);
}

public static int GetNumberOfLeadingZero(ulong x)
{
int number = 64;

ulong y = (x >> 32);
if (y != 0)
{
number -= 32;
x = y;
}

y = (x >> 16);
if (y != 0)
{
number -= 16;
x = y;
}

y = (x >> 8);
if (y != 0)
{
number -= 8;
x = y;
}

y = (x >> 4);
if (y != 0)
{
number -= 4;
x = y;
}

y = (x >> 2);
if (y != 0)
{
number -= 2;
x = y;
}

y = (x >> 1);
if (y != 0)
{
return number – 2;
}

return number – (int)x;
}

#endregion

#region Get number of leading one

public static int GetNumberOfLeadingOne(sbyte x)
{
return GetNumberOfLeadingZero(~x);
}

public static int GetNumberOfLeadingOne(short x)
{
return GetNumberOfLeadingZero(~x);
}

public static int GetNumberOfLeadingOne(int x)
{
return GetNumberOfLeadingZero(~x);
}

public static int GetNumberOfLeadingOne(long x)
{
return GetNumberOfLeadingZero(~x);
}

public static int GetNumberOfLeadingOne(byte x)
{
return GetNumberOfLeadingZero(~x);
}

public static int GetNumberOfLeadingOne(ushort x)
{
return GetNumberOfLeadingZero(~x);
}

public static int GetNumberOfLeadingOne(uint x)
{
return GetNumberOfLeadingZero(~x);
}

public static int GetNumberOfLeadingOne(ulong x)
{
return GetNumberOfLeadingZero(~x);
}

#endregion

#region Get number of tailing zero

public static int GetNumberOfTailingZero(sbyte x)
{
if (x == 0)
return 8;
int number = 7;
sbyte y = (sbyte)(x << 4); if (y != 0) { number -= 4; x = y; } y = (sbyte)(x << 2); if (y != 0) { number -= 2; x = y; } y = (sbyte)(x << 1); if (y != 0) { number--; } return number; } public static int GetNumberOfTailingZero(short x) { if (x == 0) return 16; int number = 15; short y = (short)(x << 8); if (y != 0) { number -= 8; x = y; } y = (short)(x << 4); if (y != 0) { number -= 4; x = y; } y = (short)(x << 2); if (y != 0) { number -= 2; x = y; } y = (short)(x << 1); if (y != 0) { number--; } return number; } public static int GetNumberOfTailingZero(int x) { if (x == 0) return 32; int number = 31; int y = (x << 16); if (y != 0) { number -= 16; x = y; } y = (x << 8); if (y != 0) { number -= 8; x = y; } y = (x << 4); if (y != 0) { number -= 4; x = y; } y = (x << 2); if (y != 0) { number -= 2; x = y; } y = (x << 1); if (y != 0) { number--; } return number; } public static int GetNumberOfTailingZero(long x) { if (x == 0) return 64; int number = 63; long y = (x << 32); if (y != 0) { number -= 32; x = y; } y = (x << 16); if (y != 0) { number -= 16; x = y; } y = (x << 8); if (y != 0) { number -= 8; x = y; } y = (x << 4); if (y != 0) { number -= 4; x = y; } y = (x << 2); if (y != 0) { number -= 2; x = y; } y = (x << 1); if (y != 0) { number--; } return number; } public static int GetNumberOfTailingZero(byte x) { if (x == 0) return 8; int number = 7; byte y = (byte)(x << 4); if (y != 0) { number -= 4; x = y; } y = (byte)(x << 2); if (y != 0) { number -= 2; x = y; } y = (byte)(x << 1); if (y != 0) { number--; } return number; } public static int GetNumberOfTailingZero(ushort x) { if (x == 0) return 16; int number = 15; ushort y = (ushort)(x << 8); if (y != 0) { number -= 8; x = y; } y = (ushort)(x << 4); if (y != 0) { number -= 4; x = y; } y = (ushort)(x << 2); if (y != 0) { number -= 2; x = y; } y = (ushort)(x << 1); if (y != 0) { number--; } return number; } public static int GetNumberOfTailingZero(uint x) { if (x == 0) return 32; int number = 31; uint y = (x << 16); if (y != 0) { number -= 16; x = y; } y = (x << 8); if (y != 0) { number -= 8; x = y; } y = (x << 4); if (y != 0) { number -= 4; x = y; } y = (x << 2); if (y != 0) { number -= 2; x = y; } y = (x << 1); if (y != 0) { number--; } return number; } public static int GetNumberOfTailingZero(ulong x) { if (x == 0) return 64; int number = 63; ulong y = (x << 32); if (y != 0) { number -= 32; x = y; } y = (x << 16); if (y != 0) { number -= 16; x = y; } y = (x << 8); if (y != 0) { number -= 8; x = y; } y = (x << 4); if (y != 0) { number -= 4; x = y; } y = (x << 2); if (y != 0) { number -= 2; x = y; } y = (x << 1); if (y != 0) { number--; } return number; } #endregion #region Get number of tailing one public static int GetNumberOfTailingOne(sbyte x) { return GetNumberOfTailingOne(~x); } public static int GetNumberOfTailingOne(short x) { return GetNumberOfTailingOne(~x); } public static int GetNumberOfTailingOne(int x) { return GetNumberOfTailingOne(~x); } public static int GetNumberOfTailingOne(long x) { return GetNumberOfTailingOne(~x); } public static int GetNumberOfTailingOne(byte x) { return GetNumberOfTailingOne(~x); } public static int GetNumberOfTailingOne(ushort x) { return GetNumberOfTailingOne(~x); } public static int GetNumberOfTailingOne(uint x) { return GetNumberOfTailingOne(~x); } public static int GetNumberOfTailingOne(ulong x) { return GetNumberOfTailingOne(~x); } #endregion } } [/csharp]

Clone a byte array

   
 
//http://www.bouncycastle.org/
//MIT X11 License


using System;
using System.Text;

namespace Org.BouncyCastle.Utilities
{

    /// <summary> General array utilities.</summary>
    public sealed class Arrays
    {
        private Arrays()
        {
        }


    public static byte[] Clone(byte[] data)
    {
      return data == null ? null : (byte[]) data.Clone();
    }

    public static int[] Clone(int[] data)
    {
      return data == null ? null : (int[]) data.Clone();
    }
  }
}

   
     


Get hash code for a byte array

   
 
//http://www.bouncycastle.org/
//MIT X11 License


using System;
using System.Text;

namespace Org.BouncyCastle.Utilities
{

    /// <summary> General array utilities.</summary>
    public sealed class Arrays
    {
    public static int GetHashCode(byte[] data)
    {
      if (data == null)
      {
        return 0;
      }

      int i = data.Length;
      int hc = i + 1;

      while (--i >= 0)
      {
        hc *= 257;
        hc ^= data[i];
      }

      return hc;
    }
    }
}

   
     


Binary Network Byte Order


   
  
using System;
using System.Net;
using System.Text;

public class BinaryNetworkByteOrder
{
   public static void Main()
   {
      short test1 = 45;
      int test2 = 314159;
      long test3 = -123456789033452;
      byte[] data = new byte[1024];
      string output;

      data = BitConverter.GetBytes(test1);
      output = BitConverter.ToString(data);
      Console.WriteLine("test1 = {0}, string = {1}", test1, output);

      data = BitConverter.GetBytes(test2);
      output = BitConverter.ToString(data);
      Console.WriteLine("test2 = {0}, string = {1}", test2, output);

      data = BitConverter.GetBytes(test3);
      output = BitConverter.ToString(data);
      Console.WriteLine("test3 = {0}, string = {1}", test3, output);

      short test1b = IPAddress.HostToNetworkOrder(test1);
      data = BitConverter.GetBytes(test1b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test1 = {0}, nbo = {1}", test1b, output);

      int test2b = IPAddress.HostToNetworkOrder(test2);
      data = BitConverter.GetBytes(test2b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test2 = {0}, nbo = {1}", test2b, output);

      long test3b = IPAddress.HostToNetworkOrder(test3);
      data = BitConverter.GetBytes(test3b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test3 = {0}, nbo = {1}", test3b, output);
   }
}