/// <summary> /// Unpack UInt64 object into AmiBroker DateTime /// </summary> static DateTime UnpackDate(ulong date) { // lower 32 bits var ft = BitVector32.CreateSection(1); var rs = BitVector32.CreateSection(23, ft); var ms = BitVector32.CreateSection(999, rs); var ml = BitVector32.CreateSection(999, ms); var sc = BitVector32.CreateSection(59, ml); var bv1 = new BitVector32((int)(date << 32 >> 32)); // higher 32 bits var mi = BitVector32.CreateSection(59); var hr = BitVector32.CreateSection(23, mi); var dy = BitVector32.CreateSection(31, hr); var mn = BitVector32.CreateSection(12, dy); var yr = BitVector32.CreateSection(4095, mn); var bv2 = new BitVector32((int)(date >> 32)); var hour = bv2[hr]; var minute = bv2[mi]; var second = bv1[sc]; var milsec = bv1[ml]; if (hour > 24 || minute > 59 || second > 59 || milsec > 999) { return(new DateTime(bv2[yr], bv2[mn], bv2[dy])); } return(new DateTime(bv2[yr], bv2[mn], bv2[dy], hour, minute, second, milsec)); }
public Tuple <ushort, double>[] GetAverages(ushort[] data) { Dictionary <ushort, SensorData> sensors = new Dictionary <ushort, SensorData>(); foreach (var d in data) { BitVector32 item = new BitVector32(d); BitVector32.Section checksum = BitVector32.CreateSection(1); BitVector32.Section sensorData = BitVector32.CreateSection(2067, checksum); BitVector32.Section sensorCode = BitVector32.CreateSection(7, sensorData); if (ChecksumOK(item[checksum], d >> 1)) { ushort code = (ushort)item[sensorCode]; if (!sensors.ContainsKey(code)) { sensors.Add(code, new SensorData { Sum = 0, Count = 0 }); } sensors[code].Sum += item[sensorData]; sensors[code].Count++; } } Tuple <ushort, double>[] result = new Tuple <ushort, double> [sensors.Count]; int index = 0; foreach (var sensor in sensors) { result[index++] = new Tuple <ushort, double>(sensor.Key, (double)sensor.Value.Sum / sensor.Value.Count); } return(result); }
/// <summary> /// Data used for testing unequal sections. /// </summary> /// Format is: /// 1. Section left /// 2. Section right /// <returns>Row of data</returns> public static IEnumerable <object[]> Section_Unequal_Data() { BitVector32.Section original = BitVector32.CreateSection(16); BitVector32.Section nested = BitVector32.CreateSection(16, original); yield return(new object[] { original, BitVector32.CreateSection(1) }); yield return(new object[] { BitVector32.CreateSection(1), original }); yield return(new object[] { original, nested }); yield return(new object[] { nested, original }); yield return(new object[] { nested, BitVector32.CreateSection(1, BitVector32.CreateSection(short.MaxValue)) }); yield return(new object[] { BitVector32.CreateSection(1, BitVector32.CreateSection(short.MaxValue)), nested }); yield return(new object[] { nested, BitVector32.CreateSection(16, BitVector32.CreateSection(short.MaxValue)) }); yield return(new object[] { BitVector32.CreateSection(16, BitVector32.CreateSection(short.MaxValue)), nested }); yield return(new object[] { nested, BitVector32.CreateSection(1, original) }); yield return(new object[] { BitVector32.CreateSection(1, original), nested }); }
public void Test01() { BitVector32.Section section1; BitVector32.Section section2; int code1 = 0; // HashCode of section1 int code2 = 0; // HashCode of section2 short maxValue = 0; System.Random random = new System.Random(-55); // [] two BitVectors that are the same should return the same HashCode //----------------------------------------------------------------- maxValue = (Int16)random.Next(1, Int16.MaxValue); section1 = BitVector32.CreateSection(maxValue); code1 = section1.GetHashCode(); code2 = section1.GetHashCode(); if (code1 != code2) { Assert.False(true, string.Format("Error, HashCodes of the same section: {0} != {1}", code1, code2)); } maxValue = (Int16)random.Next(1, Int16.MaxValue); section1 = BitVector32.CreateSection(maxValue); section2 = BitVector32.CreateSection(maxValue); code1 = section1.GetHashCode(); code2 = section2.GetHashCode(); if (code1 != code2) { Assert.False(true, string.Format("Error, HashCodes of different sections with same maxvalue: {0} != {1}", code1, code2)); } }
static void Main(string[] args) { //для того чтобы сделаь герлянду и чтобы лампочки менялись одновременно - нужно использовать то секции. BitVector32.Section firstSection = BitVector32.CreateSection(10); // 0xA Hex - 1010 Bin //4 байта BitVector32.Section secondSection = BitVector32.CreateSection(50, firstSection); // 0x32 Hex - 110010 Bin BitVector32.Section thirdSection = BitVector32.CreateSection(500, secondSection); // 0x1F4 Hex 111110100 Bin BitVector32.Section fourthSection = BitVector32.CreateSection(500, thirdSection); var packedBits = new BitVector32(0); packedBits[firstSection] = 10; // ....000000000000001010 packedBits[secondSection] = 50; // ....000000001100101010 //т.е добавилось 110010 после 1010 packedBits[thirdSection] = 500; //и так далее packedBits[fourthSection] = 499; //и так далее Console.WriteLine(packedBits[firstSection]); //выведет 10 Console.WriteLine(packedBits[secondSection]); //выведет 50 Console.WriteLine(packedBits[thirdSection]); //выведет 500 Console.WriteLine(packedBits[fourthSection]); //выведет 499 Console.WriteLine(packedBits); //то, что нас интересует Console.WriteLine(packedBits.Data); //выводится число, которое представляет собой последовательность бит. //это число нам ничего не говорит //Delay Console.ReadKey(); }
/// <summary> /// Data used for testing setting/unsetting via sections. /// </summary> /// Format is: /// 1. value /// 2. section /// <returns>Row of data</returns> public static IEnumerable <object[]> Section_Set_Data() { yield return(new object[] { 0, BitVector32.CreateSection(1) }); yield return(new object[] { 1, BitVector32.CreateSection(1) }); yield return(new object[] { 0, BitVector32.CreateSection(short.MaxValue) }); yield return(new object[] { short.MaxValue, BitVector32.CreateSection(short.MaxValue) }); yield return(new object[] { 0, BitVector32.CreateSection(1, BitVector32.CreateSection(byte.MaxValue)) }); yield return(new object[] { 1, BitVector32.CreateSection(1, BitVector32.CreateSection(byte.MaxValue)) }); yield return(new object[] { 0, BitVector32.CreateSection(short.MaxValue, BitVector32.CreateSection(byte.MaxValue)) }); yield return(new object[] { short.MaxValue, BitVector32.CreateSection(short.MaxValue, BitVector32.CreateSection(byte.MaxValue)) }); yield return(new object[] { 16, BitVector32.CreateSection(short.MaxValue) }); yield return(new object[] { 16, BitVector32.CreateSection(short.MaxValue, BitVector32.CreateSection(byte.MaxValue)) }); yield return(new object[] { 31, BitVector32.CreateSection(short.MaxValue) }); yield return(new object[] { 31, BitVector32.CreateSection(short.MaxValue, BitVector32.CreateSection(byte.MaxValue)) }); yield return(new object[] { 16, BitVector32.CreateSection(byte.MaxValue) }); yield return(new object[] { 16, BitVector32.CreateSection(byte.MaxValue, BitVector32.CreateSection(byte.MaxValue, BitVector32.CreateSection(short.MaxValue))) }); yield return(new object[] { 31, BitVector32.CreateSection(byte.MaxValue) }); yield return(new object[] { 31, BitVector32.CreateSection(byte.MaxValue, BitVector32.CreateSection(byte.MaxValue, BitVector32.CreateSection(short.MaxValue))) }); }
public void Indexers() { BitVector32 b = new BitVector32(7); Assert.IsTrue(b [0], "#1"); Assert.IsTrue(b [1], "#2"); Assert.IsTrue(b [2], "#3"); Assert.IsTrue(b [4], "#4"); Assert.IsTrue(!b [8], "#5"); Assert.IsTrue(!b [16], "#6"); b [8] = true; Assert.IsTrue(b [4], "#7"); Assert.IsTrue(b [8], "#8"); Assert.IsTrue(!b [16], "#9"); b [8] = false; Assert.IsTrue(b [4], "#10"); Assert.IsTrue(!b [8], "#11"); Assert.IsTrue(!b [16], "#12"); BitVector32.Section s = BitVector32.CreateSection(31); s = BitVector32.CreateSection(64, s); // Print (s); // b = new BitVector32 (0x777777); BitVector32 b1 = new BitVector32(0xffff77); BitVector32 b2 = new BitVector32(b1 [s]); //Console.WriteLine (b1.ToString ()); //Console.WriteLine (b2.ToString ()); Assert.AreEqual(123, b1 [s], "#14"); // b1 [s] = 15; //Console.WriteLine (b1.ToString ()); }
static PlanetManager() { LowBitsMask = BitVector32.CreateSection(0xFF); HighBitsMask = BitVector32.CreateSection(0xFF, LowBitsMask); LongitudeMask = BitVector32.CreateSection(1); //The bit 0 indicates if the longitude is display or not LatitudeMask = BitVector32.CreateSection(1, LongitudeMask); //The bit 1 indicates if the latitude is on DistanceMask = BitVector32.CreateSection(1, LatitudeMask); //The bit 2 indicates if the distance is on LongitudeVelocitiesMask = BitVector32.CreateSection(1, DistanceMask); //The bit 3 indicates if the longitude velocity is display or not LatitudeVelocitiesMask = BitVector32.CreateSection(1, LongitudeVelocitiesMask); //The bit 4 indicates if the latitude velocity is display or not DistanceVelocitiesMask = BitVector32.CreateSection(1, LatitudeVelocitiesMask); //The bit 5 indicates if the distance velocity is display or not AscendingMask = BitVector32.CreateSection(1, DistanceVelocitiesMask); //The bit 6 indicates if the Ascending is displayed or not DescendingMask = BitVector32.CreateSection(1, AscendingMask); //The bit 7 indicates if the Descending is displayed or not PerigeeMask = BitVector32.CreateSection(1, DescendingMask); //The bit 8 indicates if the Perigee is displayed or not ApogeeMask = BitVector32.CreateSection(1, PerigeeMask); //The bit 9 indicates if the Apogee is displayed or not AscendingLatitudeMask = BitVector32.CreateSection(1, ApogeeMask); //The bit 10 indicates if the AscendingLatitude is displayed or not DescendingLatitudeMask = BitVector32.CreateSection(1, AscendingLatitudeMask); //The bit 11 indicates if the DescendingLatitude is displayed or not PerigeeLatitudeMask = BitVector32.CreateSection(1, DescendingLatitudeMask); //The bit 12 indicates if the PerigeeLatitude is displayed or not ApogeeLatitudeMask = BitVector32.CreateSection(1, PerigeeLatitudeMask); //The bit 13 indicates if the ApogeeLatitude is displayed or not AscendingVelocitiesMask = BitVector32.CreateSection(1, ApogeeLatitudeMask); //The bit 14 indicates if the AscendingVelocities is displayed or not DescendingVelocitiesMask = BitVector32.CreateSection(1, AscendingVelocitiesMask); //The bit 15 indicates if the DescendingVelocities is displayed or not PerigeeVelocitiesMask = BitVector32.CreateSection(1, DescendingVelocitiesMask); //The bit 16 indicates if the PerigeeVelocities is displayed or not ApogeeVelocitiesMask = BitVector32.CreateSection(1, PerigeeVelocitiesMask); //The bit 17 indicates if the ApogeeVelocities is displayed or not }
static ElevatorState() { AllKeys = (Elements[])Enum.GetValues(typeof(Elements)); int itemCount = AllKeys.Count; sections = new BitVector32.Section[itemCount]; BitVector32.Section last; sections[0] = last = BitVector32.CreateSection(TopFloor); var doneState = new BitVector32(); for (int i = 1; i < itemCount; i++) { sections[i] = last = BitVector32.CreateSection(TopFloor, last); } for (int i = 0; i < ElementCount * 2; i++) { doneState[sections[i]] = TopFloor; } doneState[sections[(int)Elements.Human]] = TopFloor; DoneState = doneState.Data; for (int floor = 0; floor < 4; floor++) { var floorState = new BitVector32(); for (int i = 0; i < ElementCount; i++) { floorState[sections[(i << 1) + GeneratorOffset]] = floor; } FloorCheck[floor] = floorState.Data; } }
public static void Main() { // Store and access individual bit values using masks BitVector32 bv1 = new BitVector32(0); Console.WriteLine("bv1 intial value: \t" + (Convert.ToString(bv1.Data, 2)).PadLeft(32, '0')); int bit1 = BitVector32.CreateMask(); int bit2 = BitVector32.CreateMask(bit1); int bit3 = BitVector32.CreateMask(bit2); bv1[bit1] = true; bv1[bit3] = true; Console.WriteLine("bv1 b1 and b3 true: \t" + (Convert.ToString(bv1.Data, 2)).PadLeft(32, '0')); Console.WriteLine("------------------------------------------------------"); // Store and access larger values using sections BitVector32 bv2 = new BitVector32(0); Console.WriteLine("bv2 intial value: \t" + (Convert.ToString(bv2.Data, 2)).PadLeft(32, '0')); BitVector32.Section s1 = BitVector32.CreateSection(8); // uses first 4 bits BitVector32.Section s2 = BitVector32.CreateSection(4, s1); // uses next 3 bits following s1 bv2[s1] = 6; bv2[s2] = 4; Console.WriteLine("bv2 s1 and s2: \t\t" + (Convert.ToString(bv2.Data, 2)).PadLeft(32, '0')); }
public void Indexers() { var bitVector = new BitVector32((uint)7); Assert.IsTrue(bitVector[0]); Assert.IsTrue(bitVector[1]); Assert.IsTrue(bitVector[2]); Assert.IsTrue(bitVector[4]); Assert.IsTrue(!bitVector[8]); Assert.IsTrue(!bitVector[16]); bitVector[8] = true; Assert.IsTrue(bitVector[4]); Assert.IsTrue(bitVector[8]); Assert.IsTrue(!bitVector[16]); bitVector[8] = false; Assert.IsTrue(bitVector[4]); Assert.IsTrue(!bitVector[8]); Assert.IsTrue(!bitVector[16]); var section = BitVector32.CreateSection(31); section = BitVector32.CreateSection(64, section); var bitVector1 = new BitVector32((uint)0xffff77); var bitVector2 = new BitVector32((uint)bitVector1[section]); Assert.AreEqual(123, bitVector1[section]); }
public void SectionIncorrectSize() { var section1 = BitVector32.CreateSection(32767); var section2 = BitVector32.CreateSection(32767, section1); BitVector32.CreateSection(4, section2); }
/// <summary> /// Data used for testing equal sections. /// </summary> /// Format is: /// 1. Section left /// 2. Section right /// <returns>Row of data</returns> public static IEnumerable <object[]> Section_Equal_Data() { BitVector32.Section original = BitVector32.CreateSection(16); BitVector32.Section nested = BitVector32.CreateSection(16, original); yield return(new object[] { original, original }); yield return(new object[] { original, BitVector32.CreateSection(16) }); yield return(new object[] { BitVector32.CreateSection(16), original }); // Since the max value is changed to an inclusive mask, equal to mask max value yield return(new object[] { original, BitVector32.CreateSection(31) }); yield return(new object[] { nested, nested }); yield return(new object[] { nested, BitVector32.CreateSection(16, original) }); yield return(new object[] { BitVector32.CreateSection(16, original), nested }); yield return(new object[] { nested, BitVector32.CreateSection(31, original) }); yield return(new object[] { nested, BitVector32.CreateSection(16, BitVector32.CreateSection(16)) }); yield return(new object[] { BitVector32.CreateSection(16, BitVector32.CreateSection(16)), nested }); yield return(new object[] { nested, BitVector32.CreateSection(31, BitVector32.CreateSection(16)) }); // Because it only stores the offset, and not the previous section, later sections may be equal yield return(new object[] { nested, BitVector32.CreateSection(16, BitVector32.CreateSection(8, BitVector32.CreateSection(1))) }); yield return(new object[] { BitVector32.CreateSection(16, BitVector32.CreateSection(8, BitVector32.CreateSection(1))), nested }); }
static void Main() { BitVector32.Section firstSection = BitVector32.CreateSection(10); // 0xA Hex - 1010 Bin BitVector32.Section secondSection = BitVector32.CreateSection(50, firstSection); // 0x32 Hex - 110010 Bin BitVector32.Section thirdSection = BitVector32.CreateSection(500, secondSection); // 0x1F4 Hex - 111110100 Bin BitVector32.Section fourthSection = BitVector32.CreateSection(500, thirdSection); var packedBits = new BitVector32(0); packedBits[firstSection] = 10; packedBits[secondSection] = 50; packedBits[thirdSection] = 500; packedBits[fourthSection] = 499; Console.WriteLine(packedBits[firstSection]); Console.WriteLine(packedBits[secondSection]); Console.WriteLine(packedBits[thirdSection]); Console.WriteLine(packedBits[fourthSection]); Console.WriteLine(packedBits); // packedBits = {BitVector32{0000000000000 111110100 110010 1010}} Console.WriteLine(packedBits.Data); // packedBits.Data = 512 810 // Delay. Console.ReadKey(); }
public static void Set_Section_OutOfRangeTest(short maximum, int value) { { BitVector32 data = new BitVector32(); BitVector32.Section section = BitVector32.CreateSection(maximum); // In debug mode, attempting to set a value to a section that is too large triggers an assert. // There is no accompanying check in release mode, however, allowing invalid values to be set. #if DEBUG Exception e = Assert.ThrowsAny <Exception>(() => data[section] = value); Assert.Equal("DebugAssertException", e.GetType().Name); #else data[section] = value; Assert.Equal(maximum & value, data.Data); Assert.NotEqual(value, data.Data); Assert.Equal(maximum & value, data[section]); Assert.NotEqual(value, data[section]); #endif } { BitVector32 data = new BitVector32(); BitVector32.Section nested = BitVector32.CreateSection(maximum, BitVector32.CreateSection(short.MaxValue)); #if DEBUG Exception e = Assert.ThrowsAny <Exception>(() => data[nested] = value); Assert.Equal("DebugAssertException", e.GetType().Name); #else data[nested] = value; Assert.Equal((maximum & value) << 15, data.Data); Assert.NotEqual(value << 15, data.Data); Assert.Equal(maximum & value, data[nested]); Assert.NotEqual(value, data[nested]); #endif } }
public static void CreateSection_NextTest(short maximum, short mask) { BitVector32.Section initial = BitVector32.CreateSection(short.MaxValue); BitVector32.Section section = BitVector32.CreateSection(maximum, initial); Assert.Equal(15, section.Offset); Assert.Equal(mask, section.Mask); }
/// <summary> /// Instantiates readonly PLayerSlot object from BitVector32 /// </summary> /// <param name="bits">Player slot value compressed to 32-bit integer</param> // ┌─────────────── team (false if Radiant, true if Dire). // │ ┌─┬─┬─┬─────── not used. // │ │ │ │ │ ┌─┬─┬─ the position of a player within their team (0-4). // │ │ │ │ │ │ │ │ // 0 0 0 0 0 0 0 0 public PlayerSlot(int value) { BitVector32 bits = new BitVector32(value); TeamPosition = (uint)(bits[BitVector32.CreateSection(4)] + 1); IsDire = bits[128]; }
public void SectionIncorrectSize() { BitVector32.Section s1 = BitVector32.CreateSection(32767); BitVector32.Section s2 = BitVector32.CreateSection(32767, s1); BitVector32.Section s3 = BitVector32.CreateSection(4, s2); }
public void InitFromBytes(byte[] data) { byte[] modulationDataByteArray = new byte[4]; byte[] otherDataByteArray = new byte[4]; Buffer.BlockCopy(data, 0, modulationDataByteArray, 0, 4); Buffer.BlockCopy(data, 4, otherDataByteArray, 0, 4); this.modulationData = BitConverter.ToUInt32(modulationDataByteArray, 0); BitVector32 tempBitVector = new BitVector32(BitConverter.ToInt32(otherDataByteArray, 0)); BitVector32.Section punchthroughAlphaSection = BitVector32.CreateSection(1); BitVector32.Section colorASection = BitVector32.CreateSection(16383 /*(1 << 14) - 1*/, punchthroughAlphaSection); BitVector32.Section colorAIsOpaque = BitVector32.CreateSection(1, colorASection); BitVector32.Section colorBSection = BitVector32.CreateSection(32767 /*(1 << 15) - 1*/, colorAIsOpaque); BitVector32.Section colorBIsOpaque = BitVector32.CreateSection(1, colorBSection); if (tempBitVector[punchthroughAlphaSection] == 1) { this.usePunchthroughAlpha = true; } this.colorA = (uint)tempBitVector[colorASection]; if (tempBitVector[colorAIsOpaque] == 1) { this.colorAIsOpaque = true; } this.colorB = (uint)tempBitVector[colorBSection]; if (tempBitVector[colorBIsOpaque] == 1) { this.colorBIsOpaque = true; } }
public TexturePageInfo() { vector = new BitVector32(0); page_x = BitVector32.CreateSection(15); page_y = BitVector32.CreateSection(1, page_x); blending_mode = BitVector32.CreateSection(3, page_y); depth = BitVector32.CreateSection(3, blending_mode); }
public PaletteEntry() { vector = new BitVector32(0); red = BitVector32.CreateSection(31); green = BitVector32.CreateSection(31, red); blue = BitVector32.CreateSection(31, green); stp = BitVector32.CreateSection(1, blue); }
private static BitVector32.Section partner; //The bits 16-31 together identify the planet pair static RelationKind() { reserved = BitVector32.CreateSection(0xFFF); aspectType = BitVector32.CreateSection(15, reserved); exterior = BitVector32.CreateSection(255, aspectType); interior = BitVector32.CreateSection(255, exterior); partner = BitVector32.CreateSection(Int16.MaxValue, aspectType); }
static OperationA() { r_Sections = new BitVector32.Section[4]; r_Sections[0] = BitVector32.CreateSection(36); r_Sections[1] = BitVector32.CreateSection(6, r_Sections[0]); r_Sections[2] = BitVector32.CreateSection(24, r_Sections[1]); r_Sections[3] = BitVector32.CreateSection(12, r_Sections[2]); }
public DCB() { this.DCBlength = (uint)Marshal.SizeOf(this); this.Control = new BitVector32(0); this.sect1 = BitVector32.CreateSection(15); this.DTRsect = BitVector32.CreateSection(3, this.sect1); this.sect2 = BitVector32.CreateSection(0x3f, this.DTRsect); this.RTSsect = BitVector32.CreateSection(3, this.sect2); }
/// <summary> /// Ctor /// </summary> /// <param name="foo"></param> public Rgb16(int foo) { // allocate the bitfield buffer = new BitVector32(0); // initialize bitfield sections r = BitVector32.CreateSection(0x0f); // 4 g = BitVector32.CreateSection(0x1f, r); // 5 b = BitVector32.CreateSection(0x0f, g); // 4 }
/// <summary> /// Ctor /// </summary> /// <param name="foo"></param> public MyUnion(int foo) { // allocate the bitfield info = new BitVector32(0); // initialize bitfield sections flag1 = BitVector32.CreateSection(1); flag2 = BitVector32.CreateSection(1, flag1); flag3 = BitVector32.CreateSection(1, flag2); }
public void SectionIncorrectSize() { BitVector32.Section s1 = BitVector32.CreateSection(32767); BitVector32.Section s2 = BitVector32.CreateSection(32767, s1); try { BitVector32.Section s3 = BitVector32.CreateSection(4, s2); Assert.Fail("Illegal section created"); } catch (ArgumentException) {} }
public void SectionCorrectSize() { BitVector32.Section s1 = BitVector32.CreateSection(32767); BitVector32.Section s2 = BitVector32.CreateSection(32767, s1); BitVector32.Section s3 = BitVector32.CreateSection(3, s2); BitVector32 v1 = new BitVector32(0); v1[s3] = 3; Assert.AreEqual(v1[s3], 3); }
public void SetSendVar(int vall, int valnum) { for (var i = 0; i < dins.Length; i++) { dins[i] = new BitVector32(); } dins[valnum][BitVector32.CreateSection(100)] = vall; sendBuf(); }
private static BitVector32.Section yearMask; //The bits 20 - 31 : 12 bits static PoboDayStructure() { minutesMask = BitVector32.CreateSection(0x3F); hoursMask = BitVector32.CreateSection(0x1F, minutesMask); dayMask = BitVector32.CreateSection(0x1F, hoursMask); monthMask = BitVector32.CreateSection(0xF, dayMask); yearMask = BitVector32.CreateSection(0xFFF, monthMask); Size = Marshal.SizeOf(typeof(PoboDayStructure)); }