Ejemplo n.º 1
0
        public static byte ByteAt(this byte data, int pos)
        {
#if BITSTACK_DEBUG
            if (pos != 0)
            {
                BitDebug.Exception("byte.ByteAt(int) - position must be between 0 and 0 but was " + pos);
            }
#endif
            return(data);
        }
Ejemplo n.º 2
0
        public static int ToggleBitAt(this int data, int pos)
        {
#if BITSTACK_DEBUG
            if (pos < 0 || pos > 31)
            {
                BitDebug.Exception("int.ToggleBitAt(int) - position must be between 0 and 31 but was " + pos);
            }
#endif
            return(data ^ (1 << pos));
        }
Ejemplo n.º 3
0
        public static byte ToggleBitAt(this byte data, int pos)
        {
#if BITSTACK_DEBUG
            if (pos < 0 || pos > 7)
            {
                BitDebug.Exception("byte.ToggleBitAt(int) - position must be between 0 and 7 but was " + pos);
            }
#endif
            return((byte)(data ^ (1 << pos)));
        }
Ejemplo n.º 4
0
 public static uint SetBitAt(this uint data, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos < 0 || pos > 31)
     {
         BitDebug.Exception("uint.SetBitAt(int) - position must be between 0 and 31 but was " + pos);
     }
                 #endif
     return(data | 1u << pos);
 }
Ejemplo n.º 5
0
 public static int BitAt(this short data, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos < 0 || pos > 15)
     {
         BitDebug.Exception("short.BitAt(int) - position must be between 0 and 15 but was " + pos);
     }
                 #endif
     return((data >> pos) & 1);
 }
Ejemplo n.º 6
0
 public static sbyte SetByteAt(this sbyte data, byte newData, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos != 0)
     {
         BitDebug.Exception("sbyte.SetByteAt(int) - position must be between 0 and 0 but was " + pos);
     }
                 #endif
     return((sbyte)newData);
 }
Ejemplo n.º 7
0
        public static byte ByteAt(this long data, int pos)
        {
#if BITSTACK_DEBUG
            if (pos < 0 || pos > 7)
            {
                BitDebug.Exception("long.ByteAt(int) - position must be between 0 and 7 but was " + pos);
            }
#endif
            return((byte)(data >> (56 - (pos * 8))));
        }
Ejemplo n.º 8
0
        public static int BitInvAt(this ulong data, int pos)
        {
#if BITSTACK_DEBUG
            if (pos < 0 || pos > 63)
            {
                BitDebug.Exception("ulong.BitInvAt(int) - position must be between 0 and 63 but was " + pos);
            }
#endif
            return(1 - (int)((data >> pos) & 1ul));
        }
Ejemplo n.º 9
0
 public static int BitInvAt(this sbyte data, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos < 0 || pos > 7)
     {
         BitDebug.Exception("sbyte.BitInvAt(int) - position must be between 0 and 7 but was " + pos);
     }
                 #endif
     return(1 - ((data >> pos) & 1));
 }
Ejemplo n.º 10
0
        public static ulong ToggleBitAt(this ulong data, int pos)
        {
#if BITSTACK_DEBUG
            if (pos < 0 || pos > 63)
            {
                BitDebug.Exception("ulong.ToggleBitAt(int) - position must be between 0 and 63 but was " + pos);
            }
#endif
            return(data ^ (1ul << pos));
        }
Ejemplo n.º 11
0
 public static int BitInvAt(this uint data, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos < 0 || pos > 31)
     {
         BitDebug.Exception("uint.BitInvAt(int) - position must be between 0 and 31 but was " + pos);
     }
                 #endif
     return(1 - (int)((data >> pos) & 1));
 }
Ejemplo n.º 12
0
        public static long SetBitAt(this long data, int pos)
        {
#if BITSTACK_DEBUG
            if (pos < 0 || pos > 63)
            {
                BitDebug.Exception("long.SetBitAt(int) - position must be between 0 and 63 but was " + pos);
            }
#endif
            return(data | 1L << pos);
        }
Ejemplo n.º 13
0
 public static short ToggleBitAt(this short data, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos < 0 || pos > 15)
     {
         BitDebug.Exception("short.ToggleBitAt(int) - position must be between 0 and 15 but was " + pos);
     }
                 #endif
     return((short)(data ^ (1 << pos)));
 }
Ejemplo n.º 14
0
 public static sbyte SetBitAt(this sbyte data, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos < 0 || pos > 7)
     {
         BitDebug.Exception("sbyte.SetBitAt(int) - position must be between 0 and 7 but was " + pos);
     }
                 #endif
     return((sbyte)((byte)data | 1 << pos));
 }
Ejemplo n.º 15
0
 public static byte ByteAt(this uint data, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos < 0 || pos > 3)
     {
         BitDebug.Exception("uint.ByteAt(int) - position must be between 0 and 3 but was " + pos);
     }
                 #endif
     return((byte)(data >> (24 - (pos * 8))));
 }
Ejemplo n.º 16
0
 public static ushort SetBitAt(this ushort data, int pos)
 {
                 #if BITSTACK_DEBUG
     if (pos < 0 || pos > 15)
     {
         BitDebug.Exception("ushort.SetBitAt(int) - position must be between 0 and 15 but was " + pos);
     }
                 #endif
     return((ushort)(data | 1u << pos));
 }
Ejemplo n.º 17
0
        public LinearKey3(int linearKey)
        {
#if BITSTACK_DEBUG
            if (linearKey < 0)
            {
                BitDebug.Exception("LinearKey3(int) - linear key must be positive");
            }
#endif

            this.linearKey = (uint)linearKey;
        }
Ejemplo n.º 18
0
        public MortonKey3(int mortonKey)
        {
                        #if BITSTACK_DEBUG
            if (mortonKey < 0)
            {
                BitDebug.Exception("MortonKey3(int) - morton key must be positive");
            }
                        #endif

            this.mortonKey = (uint)mortonKey;
        }
Ejemplo n.º 19
0
        public static long SetByteAt(this long data, byte newData, int pos)
        {
#if BITSTACK_DEBUG
            if (pos < 0 || pos > 7)
            {
                BitDebug.Exception("long.SetByteAt(int) - position must be between 0 and 7 but was " + pos);
            }
#endif
            int  shift = 56 - (pos * 8);
            long mask  = (long)0xFF << shift;
            long m1    = ((long)newData << shift) & mask;
            long m2    = data & ~mask;

            return(m2 | m1);
        }
Ejemplo n.º 20
0
        public static uint SetByteAt(this uint data, byte newData, int pos)
        {
                        #if BITSTACK_DEBUG
            if (pos < 0 || pos > 3)
            {
                BitDebug.Exception("uint.SetByteAt(int) - position must be between 0 and 3 but was " + pos);
            }
                        #endif
            int  shift = 24 - (pos * 8);
            uint mask  = (uint)(0xFF << shift);
            uint m1    = ((uint)newData << shift) & mask;
            uint m2    = data & ~mask;

            return(m2 | m1);
        }
Ejemplo n.º 21
0
        public static ushort SetByteAt(this ushort data, byte newData, int pos)
        {
                        #if BITSTACK_DEBUG
            if (pos < 0 || pos > 1)
            {
                BitDebug.Exception("ushort.SetByteAt(int) - position must be between 0 and 1 but was " + pos);
            }
                        #endif
            int shift = 8 - (pos * 8);
            int mask  = 0xFF << shift;
            int m1    = (newData << shift) & mask;
            int m2    = data & ~mask;

            return((ushort)(m2 | m1));
        }
Ejemplo n.º 22
0
        public static ulong ULongFromBitString(this string data, int readIndex)
        {
#if BITSTACK_DEBUG
            if ((readIndex + 64) > data.Length)
            {
                BitDebug.Exception("string.ULongFromBitString(int) - read index and ulong length is less than the string size");
            }
#endif
            ulong value = 0;

            for (int i = readIndex, j = 63; i < 64; i++, j--)
            {
                value = data[i] == '1' ? value.SetBitAt(j) : value.UnsetBitAt(j);
            }

            return(value);
        }
Ejemplo n.º 23
0
        public static byte ByteFromBitString(this string data, int readIndex)
        {
#if BITSTACK_DEBUG
            if ((readIndex + 8) > data.Length)
            {
                BitDebug.Exception("string.ByteFromBitString(int) - read index and byte length is less than the string size");
            }
#endif
            byte value = 0;

            for (int i = readIndex, j = 7; i < 8; i++, j--)
            {
                value = data[i] == '1' ? value.SetBitAt(j) : value.UnsetBitAt(j);
            }

            return(value);
        }
Ejemplo n.º 24
0
        public static uint UIntFromBitString(this string data, int readIndex)
        {
                        #if BITSTACK_DEBUG
            if ((readIndex + 32) > data.Length)
            {
                BitDebug.Exception("string.UIntFromBitString(int) - read index and uint length is less than the string size");
            }
                        #endif
            uint value = 0;

            for (int i = readIndex, j = 31; i < 32; i++, j--)
            {
                value = data[i] == '1' ? value.SetBitAt(j) : value.UnsetBitAt(j);
            }

            return(value);
        }
        public static int PopCount(this uint[] data)
        {
                        #if BITSTACK_DEBUG
            if (data == null)
            {
                BitDebug.Exception("uint[].PopCount() - array is null");
            }
                        #endif

            int counter = 0;
            int length  = data.Length;

            for (int i = 0; i < length; i++)
            {
                counter += data[i].PopCount();
            }

            return(counter);
        }
Ejemplo n.º 26
0
        public static long SetBit(this long data, int pos, long bit)
        {
#if BITSTACK_DEBUG
            if (pos < 0 || pos > 63)
            {
                BitDebug.Exception("long.SetBit(int, int) - position must be between 0 and 63 but was " + pos);
            }

            if (bit != 0 && bit != 1)
            {
                BitDebug.Exception("long.SetBit(int, int) - bit value must be either 0 or 1 but was " + bit);
            }
#endif
            long mask = 1L << pos;
            long m1   = (bit << pos) & mask;
            long m2   = data & ~mask;

            return(m2 | m1);
        }
Ejemplo n.º 27
0
        public static sbyte SetBit(this sbyte data, int pos, int bit)
        {
                        #if BITSTACK_DEBUG
            if (pos < 0 || pos > 7)
            {
                BitDebug.Exception("sbyte.SetBit(int, int) - position must be between 0 and 7 but was " + pos);
            }

            if (bit != 0 && bit != 1)
            {
                BitDebug.Exception("sbyte.SetBit(int, int) - bit value must be either 0 or 1 but was " + bit);
            }
                        #endif
            int mask = 1 << pos;
            int m1   = (bit << pos) & mask;
            int m2   = data & ~mask;

            return((sbyte)(m2 | m1));
        }
Ejemplo n.º 28
0
        public static uint SetBit(this uint data, int pos, int bit)
        {
                        #if BITSTACK_DEBUG
            if (pos < 0 || pos > 31)
            {
                BitDebug.Exception("uint.SetBit(int, int) - position must be between 0 and 31 but was " + pos);
            }

            if (bit != 0 && bit != 1)
            {
                BitDebug.Exception("uint.SetBit(int, int) - bit value must be either 0 or 1 but was " + bit);
            }
                        #endif
            uint mask = 1u << pos;
            uint m1   = ((uint)bit << pos) & mask;
            uint m2   = data & ~mask;

            return(m2 | m1);
        }
Ejemplo n.º 29
0
        public LinearKey3(uint x, uint y, uint z)
        {
#if BITSTACK_DEBUG
            if (x > 1024 || x < 0)
            {
                BitDebug.Exception("LinearKey3(uint, uint, uint) - linear key x component must be between 0-1023 (10 bits), was " + x);
            }

            if (y > 1024 || y < 0)
            {
                BitDebug.Exception("LinearKey3(uint, uint, uint) - linear key y component must be between 0-1023 (10 bits), was " + y);
            }

            if (z > 1024 || z < 0)
            {
                BitDebug.Exception("LinearKey3(uint, uint, uint) - linear key z component must be between 0-1023 (10 bits), was " + z);
            }
#endif
            this.linearKey = EncodeLinearKey(x, y, z);
        }
Ejemplo n.º 30
0
        public MortonKey3(int x, int y, int z)
        {
                        #if BITSTACK_DEBUG
            if (x > 1024 || x < 0)
            {
                BitDebug.Exception("MortonKey3(int, int, int) - morton key x component must be between 0-1023 (10 bits), was " + x);
            }

            if (y > 1024 || y < 0)
            {
                BitDebug.Exception("MortonKey3(int, int, int) - morton key y component must be between 0-1023 (10 bits), was " + y);
            }

            if (z > 1024 || z < 0)
            {
                BitDebug.Exception("MortonKey3(int, int, int) - morton key z component must be between 0-1023 (10 bits), was " + z);
            }
                        #endif
            mortonKey = BitMath.EncodeMortonKey((uint)x, (uint)y, (uint)z);
        }