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)); }
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); }
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)); }
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)); }
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); }
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)))); }
public static short SetBitAt(this short data, int pos) { #if BITSTACK_DEBUG if (pos < 0 || pos > 15) { BitDebug.Exception("short.SetBitAt(int) - position must be between 0 and 15 but was " + pos); } #endif return((short)((ushort)data | 1u << pos)); }
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); }
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); }
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)))); }
public LinearKey3(int linearKey) { #if BITSTACK_DEBUG if (linearKey < 0) { BitDebug.Exception("LinearKey3(int) - linear key must be positive"); } #endif this.linearKey = (uint)linearKey; }
public MortonKey3(int mortonKey) { #if BITSTACK_DEBUG if (mortonKey < 0) { BitDebug.Exception("MortonKey3(int) - morton key must be positive"); } #endif this.mortonKey = (uint)mortonKey; }
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); }
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)); }
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); }
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 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); }
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); }
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); }
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); }
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); }
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)); }
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); }
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); }
public static void SetByteAt(this byte[] data, byte newData, int pos) { #if BITSTACK_DEBUG if (pos < 0) { BitDebug.Exception("byte[].SetByteAt(byte, int) - byte position must not be less than 0 was " + pos); } if (data == null) { BitDebug.Exception("byte[].SetByteAt(byte, int) - array is null"); } if (pos > data.Length) { BitDebug.Exception("byte[].SetByteAt(byte, int) - byte bucket must not be greater than array " + data.Length + " was " + pos); } #endif data[pos] = newData; }
public static byte ByteAt(this sbyte[] data, int pos) { #if BITSTACK_DEBUG if (pos < 0) { BitDebug.Exception("sbyte[].ByteAt(int) - byte position must not be less than 0 was " + pos); } if (data == null) { BitDebug.Exception("sbyte[].ByteAt(int) - array is null"); } if (pos > data.Length) { BitDebug.Exception("sbyte[].ByteAt(int) - byte bucket must not be greater than array " + data.Length + " was " + pos); } #endif return((byte)data[pos]); }
public static byte ByteAt(this long[] data, int pos) { int byteIndex = pos / BYTE_LEN; #if BITSTACK_DEBUG if (pos < 0) { BitDebug.Exception("long[].ByteAt(int) - byte position must not be less than 0 was " + pos); } if (data == null) { BitDebug.Exception("long[].ByteAt(int) - array is null"); } if (byteIndex > data.Length) { BitDebug.Exception("long[].ByteAt(int) - byte bucket must not be greater than array " + data.Length + " was " + byteIndex); } #endif return(data[byteIndex].ByteAt(pos % BYTE_LEN)); }
public static void SetByteAt(this uint[] data, byte newData, int pos) { int byteIndex = pos / BYTE_LEN; #if BITSTACK_DEBUG if (pos < 0) { BitDebug.Exception("uint[].SetByteAt(byte, int) - byte position must not be less than 0 was " + pos); } if (data == null) { BitDebug.Exception("uint[].SetByteAt(byte, int) - array is null"); } if (byteIndex > data.Length) { BitDebug.Exception("uint[].SetByteAt(byte, int) - byte bucket must not be greater than array " + data.Length + " was " + byteIndex); } #endif data[byteIndex] = data[byteIndex].SetByteAt(newData, pos % BYTE_LEN); }
public static void SetBit(this long[] data, int pos, int bit) { int bitIndex = pos / BIT_LEN; #if BITSTACK_DEBUG if (pos < 0) { BitDebug.Exception("long[].SetBit(int, int) - bit position must not be less than 0 was " + pos); } if (data == null) { BitDebug.Exception("long[].SetBit(int, int) - array is null"); } if (bitIndex > data.Length) { BitDebug.Exception("long[].SetBit(int, int) - bit bucket must not be greater than array " + data.Length + " was " + bitIndex); } #endif // pipe into the single value type data[bitIndex] = data[bitIndex].SetBit(pos % BIT_LEN, bit); }
public static int BitInvAt(this uint[] data, int pos) { int bitIndex = pos / BIT_LEN; #if BITSTACK_DEBUG if (pos < 0) { BitDebug.Exception("uint[].BitInvAt(int) - bit position must not be less than 0 was " + pos); } if (data == null) { BitDebug.Exception("uint[].BitInvAt(int) - array is null"); } if (bitIndex > data.Length) { BitDebug.Exception("uint[].BitInvAt(int) - bit bucket must not be greater than array " + data.Length + " was " + bitIndex); } #endif // pipe into the single value type return(data[bitIndex].BitInvAt(pos % BIT_LEN)); }