Bit Helper : Binary Bit « Data Types « C# / C Sharp






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);
        }
    }
}

   
    
  








Related examples in the same category

1.Using the Bitwise Complement Operators with Various Data TypesUsing the Bitwise Complement Operators with Various Data Types
2.Obtaining the Most Significant or Least Significant Bits of a NumberObtaining the Most Significant or Least Significant Bits of a Number
3.Binary Data TestBinary Data Test
4.Binary Network Byte OrderBinary Network Byte Order
5.Int binary
6.Get hash code for a byte array
7.Clone a byte array
8.Count the number of bit
9.Bit shifting for int and long value
10.Returns how many bits are necessary to hold a certain number