public static void DeserializeGivenChangeMask( BinaryReader reader, NetworkedComponentTypeInfo networkedComponentTypeInfo, object oldValue, uint changeMask ) { byte changeMaskBitIndex = 0; foreach (var field in networkedComponentTypeInfo.ThingsToSynchronize) { if (BitUtilities.GetBit(changeMask, changeMaskBitIndex)) { if (field.FieldInfo != null) { var newFieldValue = Deserialize( reader, field.FieldInfo.FieldType, field.IsNullableIfReferenceType, field.AreElementsNullableIfReferenceType ); field.FieldInfo.SetValue(oldValue, newFieldValue); } else if (field.PropertyInfo != null) { var newFieldValue = Deserialize( reader, field.PropertyInfo.PropertyType, field.IsNullableIfReferenceType, field.AreElementsNullableIfReferenceType ); field.PropertyInfo.SetValue(oldValue, newFieldValue); } } changeMaskBitIndex++; } }
public void TestUint128(string aStr, string bStr, byte expected) { UInt128 a = new UInt128(BigInteger.Parse(aStr)); UInt128 b = new UInt128(BigInteger.Parse(bStr)); BitUtilities.FindCommonPrefixSize(a, b).Should().Be(expected); }
private void LogReceivedStateDelta(NetworkedGameState networkedGameState) { OsFps.Logger.Log(networkedGameState.SequenceNumber); for (var i = 0; i < networkedGameState.NetworkedComponentTypeInfos.Count; i++) { var networkedComponentTypeInfo = networkedGameState.NetworkedComponentTypeInfos[i]; var networkedComponentInfos = networkedGameState.NetworkedComponentInfoLists[i]; foreach (var componentInfo in networkedComponentInfos) { var synchronizedFieldNames = BitUtilities.GetSetBitIndices(componentInfo.ChangeMask) .Select(bitIndex => { if (bitIndex >= networkedComponentTypeInfo.ThingsToSynchronize.Count) { return(null); } var thingToSynchronize = networkedComponentTypeInfo.ThingsToSynchronize[bitIndex]; var thingName = (thingToSynchronize.FieldInfo != null) ? thingToSynchronize.FieldInfo.Name : thingToSynchronize.PropertyInfo.Name; return(thingName); }) .Where(x => x != null) .ToArray(); OsFps.Logger.Log(networkedComponentTypeInfo.StateType.Name + ": " + Convert.ToString(componentInfo.ChangeMask, 2) + " | " + string.Join(", ", synchronizedFieldNames)); } } }
/// <summary> /// Get the opcode. /// </summary> /// <param name="inputBits">instruction bits</param> /// <returns>Operation</returns> public Operation DecodeOpcode(int inputBits) { // Get mask for opcode + InstructionID opcode = inputBits & BitUtilities.CreateBitMask(opcodeStartBit, 15); switch (opcode) { case (int)opcodeIdentificationHexLiterals.hexADD: return(new ADDoperation()); case (int)opcodeIdentificationHexLiterals.hexADC: return(new ADCoperation()); case (int)opcodeIdentificationHexLiterals.hexSUB: return(new SUBoperation()); case (int)opcodeIdentificationHexLiterals.hexMUL: return(new MULoperation()); case (int)opcodeIdentificationHexLiterals.hexDIV: return(new DIVoperation()); case (int)opcodeIdentificationHexLiterals.hexMOD: return(new MODoperation()); case (int)opcodeIdentificationHexLiterals.hexAND: return(new ANDoperation()); case (int)opcodeIdentificationHexLiterals.hexOR: return(new ORoperation()); case (int)opcodeIdentificationHexLiterals.hexNOT: return(new NOToperation()); case (int)opcodeIdentificationHexLiterals.hexNAND: return(new NANDoperation()); case (int)opcodeIdentificationHexLiterals.hexJMP: return(new JMPoperation()); case (int)opcodeIdentificationHexLiterals.hexJC: return(new JCoperation()); case (int)opcodeIdentificationHexLiterals.hexCMP: return(new CMPoperation()); case (int)opcodeIdentificationHexLiterals.hexNOP: return(new NOPoperation()); case (int)opcodeIdentificationHexLiterals.hexLOAD: return(new LOADoperation()); case (int)opcodeIdentificationHexLiterals.hexSTOR: return(new STORoperation()); default: throw new System.Exception("Invalid Opcode."); } }
#pragma warning disable CS0618 public IpAddressV4(IPAddress address) : this(BitUtilities.Reverse((uint)address.Address)) #pragma warning restore CS0618 { if (address.AddressFamily != AddressFamily.InterNetwork) { throw new ArgumentException(); } }
private IpAddressV6(byte[] address) : this(BitUtilities.Reverse(BitConverter.ToUInt64(address, 0)), BitUtilities.Reverse(BitConverter.ToUInt64(address, 8))) { if (address.Length != 16) { throw new ArgumentException(); } }
/// <summary> /// Decode the second operand of this instruction by maskign it and /// 1)deteriming its value /// 2)assigning it a textual meaning. /// </summary> /// <param name="inputBits"></param> private void DecodeSecondOperand(int inputBits) { operandTwoValue = BitUtilities.MaskInput(inputBits, operandTwoStartBit, operandTwoEndBit); if (operandTwoValue < 0 || operandTwoValue > 15) //used to check if valid register { operandTwoMeaning = $"OP2: Ya messed* up"; } operandTwoMeaning = $"r{operandTwoValue}"; }
public virtual void WritePort(byte port, byte data) { if (BitUtilities.IsBitSet(data, 7)) { /* LATCH/DATA byte; get channel (0-3) and type (0 is tone/noise, 1 is volume) */ latchedChannel = (byte)((data >> 5) & 0x03); latchedType = (byte)((data >> 4) & 0x01); /* Mask off non-data bits */ data &= 0x0F; /* If target is channel 3 noise (3 bits), mask off highest bit */ if (latchedChannel == 3 && latchedType == 0) { data &= 0x07; } /* Write to register */ if (latchedType == 0) { /* Data is tone/noise */ toneRegisters[latchedChannel] = (ushort)((toneRegisters[latchedChannel] & 0x03F0) | data); } else { /* Data is volume */ volumeRegisters[latchedChannel] = data; } } else { /* DATA byte; mask off non-data bits */ data &= 0x3F; /* Write to register */ if (latchedType == 0) { /* Data is tone/noise */ if (latchedChannel == 3) { /* Target is channel 3 noise, mask off excess bits and write to low bits of register */ toneRegisters[latchedChannel] = (ushort)(data & 0x07); } else { /* Target is not channel 3 noise, write to high bits of register */ toneRegisters[latchedChannel] = (ushort)((toneRegisters[latchedChannel] & 0x000F) | (data << 4)); } } else { /* Data is volume; mask off excess bits and write to low bits of register */ volumeRegisters[latchedChannel] = (ushort)(data & 0x0F); } } }
public void CurrentUlong() { ulong expected = 0x8765432155648934; ulong val = 0x3489645521436587; ulong res = BitUtilities.Reverse(val); if (res != expected) { throw new ArgumentException(); } }
public void CurrentUint() { uint expected = 0x21436587; uint val = 0x87654321; uint res = BitUtilities.Reverse(val); if (res != expected) { throw new ArgumentException(); } }
/// <summary> /// Decode value of the immediateSwitch /// </summary> /// <param name="inputBits">our instruction</param> private void DecodeImmediateSwitch(int inputBits) { immediateSwitchValue = BitUtilities.MaskInput(inputBits, immediateSwitchStartBit, immediateSwitchEndBit); if (immediateSwitchValue == (int)ImmediateSwitchEnum.register) { immediateSwitchMeaning = "Register"; } else { immediateSwitchMeaning = "Immediate"; } }
private void DecodeFirstOperand(int inputBits) { operandOneValue = inputBits & BitUtilities.CreateBitMask(operandOneStartBit, operandOneEndBit); if (operandOneValue < 0 || operandOneValue > 15) { operandTwoMeaning = $"OP1: Ya fucke* up"; } else { operandOneMeaning = $"r{operandOneValue}"; } }
/// <summary> /// Decode the first operand of this instruction by masking it and /// 1) Determining its value /// 2) Assigning it a textual meaning. (FIX THIS) /// </summary> /// <param name="inputBits">instruction bits</param> private void DecodeFirstOperand(int inputBits) { operandOneValue = BitUtilities.MaskInput(inputBits, operandOneStartBit, operandOneEndBit); if (operandOneValue < 0 || operandOneValue > 15) { //used to check if valid register operandOneMeaning = $"OP1: Ya messed up"; } else { operandOneMeaning = $"r{operandOneValue}"; } }
/// <summary> /// Decode the first operand of this instruction by masking it and /// 1) Determining its value /// 2) Assigning it a textual meaning. (FIX THIS) /// </summary> /// <param name="inputBits">instruction bits</param> private void DecodeFirstOperand(int inputBits) { operandOneValue = BitUtilities.MaskInput(inputBits, operandOneStartBit, operandOneEndBit); if (operandOneValue < 0 || operandOneValue > 15) { operandTwoMeaning = $"OP1: Ya messed* up"; } else { operandOneMeaning = $"r{operandOneValue}"; } }
/// <summary> /// Decodes the valuie at the negative bit. /// </summary> /// <param name="inputBits">bits to be used.</param> private void DecodeNegativeBit(int inputBits) { negativeBitValue = BitUtilities.MaskInput(inputBits, negativeBit, negativeBit); // Get value from negative bit. if (negativeBitValue == 0) { negativeBitMeaning = "+"; } else { negativeBitMeaning = "-"; } }
/// <summary> /// Decode value of the immediateSwitch /// </summary> /// <param name="inputBits">our instruction</param> private void DecodeImmediateSwitch(int inputBits) { immediateSwitchValue = BitUtilities.MaskInput(inputBits, immediateSwitchStartBit, immediateSwitchEndBit); if (immediateSwitchValue == 0) // if using register, this is an indirect index reference { addressingModeMeaning = "an Indexed Indirect Register"; } else { addressingModeMeaning = "a direct address."; } }
private uint ConstructMask(int denominator) { MaskBits = BitUtilities.GetNumberBitsToRepresent(denominator); uint retMask = 0; for (int i = 0; i < MaskBits; i++) { retMask |= 1; retMask <<= 1; } return(retMask >> 1); }
private void DecodeBlock(BitReader readerIn) { int numCodedSymbols = readerIn.ReadInt(23); //Change to use stack only if the output stream is non-seekable //If it is seekable, then simply write the number of bytes that will be put into the stream //Then seek backward, writing them one by one Stack <int> symbolsStack = new Stack <int>(); AnsCodingTable decodeTable = AnsCodingTable.ReadTable(readerIn, EncryptionKey); int bitCapacity = BitUtilities.GetNumberBitsToRepresent(decodeTable.Denominator); AnsState internalsState = new AnsState(bitCapacity, null, 0); internalsState.Underlying = readerIn.ReadUint(bitCapacity); KeyValuePair <int, int> symbolRow; for (int i = 0; i < numCodedSymbols; i++) { symbolRow = decodeTable.DecodePoint(internalsState.ToInt()); symbolsStack.Push(symbolRow.Key); internalsState.Underlying = (uint)symbolRow.Value; int bitsToRead = bitCapacity - internalsState.Contained; for (int j = 0; j < bitsToRead; j++) { internalsState.PushLower(readerIn.ReadBit()); } } //Decoding finished... BitWriter symbolWriter = new BitWriter(Output); byte numBitsToWrite; if (decodeTable.SymbolType == typeof(byte)) { numBitsToWrite = 8; } else if (decodeTable.SymbolType == typeof(short)) { numBitsToWrite = 16; } else { numBitsToWrite = 32; } while (symbolsStack.Count != 0) { int symbol = symbolsStack.Pop(); symbolWriter.WriteLong(symbol, numBitsToWrite); } }
public IpAddressNetworkV6(IPAddress address, byte mask) { if (address.AddressFamily != AddressFamily.InterNetworkV6) { throw new ArgumentException(); } byte[] bytes = address.GetAddressBytes(); ulong high = BitUtilities.Reverse(BitConverter.ToUInt64(bytes, 0)); ulong low = BitUtilities.Reverse(BitConverter.ToUInt64(bytes, 8)); _networkAddress = new IpAddressV6(high, low); _mask = mask; TruncateNetworkAddress(ref _networkAddress); }
/// <summary> /// Decode the first operand of this instruction by masking it and /// 1) Determining its value /// 2) Assigning it a textual meaning. (FIX THIS) /// </summary> /// <param name="inputBits">instruction bits</param> private void DecodeFirstOperand(int inputBits) { if (immediateSwitchValue == (int)ImmediateSwitchEnum.immediate) // This is an immediate value. { operandOneValue = BitUtilities.MaskInput(inputBits, immediateOperandStartBit, operandOneEndBit); operandOneMeaning = $"{operandOneValue}"; } else // this is a register value { operandOneValue = BitUtilities.MaskInput(inputBits, operandOneStartBit, operandOneEndBit); // mask it if (operandOneValue < 0 || operandOneValue > 15) // is it one of our general registers? { throw new System.Exception("Bad Operand One!"); // if not, BAD } operandOneMeaning = $"r{operandOneValue}"; } }
private void DecodeSecondOperand(int inputBits) { operandTwoValue = inputBits & BitUtilities.CreateBitMask(operandTwoStartBit, operandTwoEndBit); // Immediate or Register? if (immediateSwitchValue == 0) // if register { if (operandTwoValue < 0 || operandTwoValue > 15) { operandTwoMeaning = $"OP2: Ya fucke* up"; } operandTwoMeaning = $"r{operandTwoValue}"; } else // else is an immediate { operandTwoMeaning = $"#{operandTwoValue}"; } }
/// <summary> /// decode the second operand and get its value and meaning /// </summary> /// <param name="inputBits">instruction bits</param> private void DecodeSecondOperand(int inputBits) { // Immediate or Register? if (immediateSwitchValue == (int)ImmediateSwitchEnum.immediate) // This is an immediate value. { operandTwoValue = BitUtilities.MaskInput(inputBits, immediateOperandStartBit, operandTwoEndBit); operandTwoMeaning = $"#{operandTwoValue}"; } else { operandTwoValue = BitUtilities.MaskInput(inputBits, operandTwoStartBit, operandTwoEndBit); if (operandTwoValue < 0 || operandTwoValue > 15) { operandTwoMeaning = $"OP2: Ya messed up"; } operandTwoMeaning = $"r{operandTwoValue}"; } }
/// <summary> /// used to decode the second operand and get its meaning and value /// </summary> /// <param name="inputBits">instruction bits</param> private void DecodeSecondOperand(int inputBits) { operandTwoValue = BitUtilities.MaskInput(inputBits, operandTwoStartBit, operandTwoEndBit); // Immediate or Register? if (immediateSwitchValue == 0) // if register { if (operandTwoValue < 0 || operandTwoValue > 15) { //used to check if valid register operandTwoMeaning = $"OP2: Ya messed up"; } operandTwoMeaning = $"r{operandTwoValue}"; } else // else is an immediate { operandTwoMeaning = $"#{operandTwoValue}"; } }
/// <summary> /// Decodes the conditional that we look for with this statement. /// </summary> /// <param name="inputBits">instruction bits</param> private void DecodeConditionalValue(int inputBits) { conditionalValue = BitUtilities.MaskInput(inputBits, conditionalStartBit, conditionalEndBit); switch (conditionalValue) { case 0: conditionalMeaning = "Equal to"; break; case 1: conditionalMeaning = "Not Equal to"; break; case 2: conditionalMeaning = "greater than"; break; case 3: conditionalMeaning = "less than"; break; case 4: conditionalMeaning = "above or equal to"; break; case 5: conditionalMeaning = "Below or equal to"; break; default: throw new System.Exception("Invalid Conditional!"); } }
public static uint GetChangeMask( NetworkedComponentTypeInfo networkedComponentTypeInfo, object newValue, object oldValue ) { Assert.IsTrue(networkedComponentTypeInfo.ThingsToSynchronize.Count <= (8 * sizeof(uint))); if (oldValue == null) { return(uint.MaxValue); } uint changeMask = 0; uint changeMaskBitIndex = 0; foreach (var field in networkedComponentTypeInfo.ThingsToSynchronize) { object oldFieldValue, newFieldValue; if (field.FieldInfo != null) { oldFieldValue = field.FieldInfo.GetValue(oldValue); newFieldValue = field.FieldInfo.GetValue(newValue); } else if (field.PropertyInfo != null) { oldFieldValue = field.PropertyInfo.GetValue(oldValue); newFieldValue = field.PropertyInfo.GetValue(newValue); } else { throw new Exception("Invalid field to synchronize."); } BitUtilities.SetBit(ref changeMask, (byte)changeMaskBitIndex, !object.Equals(newFieldValue, oldFieldValue)); changeMaskBitIndex++; } return(changeMask); }
public static IpAddressNetworkV6 MakeSupernet(IEnumerable <IpAddressNetworkV6> others) { bool hadAny = false; byte shortestMask = 128; UInt128 final = UInt128.MaxValue; foreach (IpAddressNetworkV6 other in others) { final &= other._networkAddress.Address; byte lowestCommon = BitUtilities.FindCommonPrefixSize(final, other._networkAddress.Address); shortestMask = Math.Min(shortestMask, lowestCommon); hadAny = true; } if (!hadAny) { throw new ArgumentException("Input was empty", nameof(others)); } return(new IpAddressNetworkV6(new IpAddressV6(final), shortestMask)); }
public static IpAddressNetworkV4 MakeSupernet(IEnumerable <IpAddressNetworkV4> others) { byte shortestMask = 32; bool hadAny = false; uint final = uint.MaxValue; foreach (IpAddressNetworkV4 range in others) { final &= range._networkAddress.Address; byte lowestCommon = BitUtilities.FindCommonPrefixSize(final, range._networkAddress.Address); shortestMask = Math.Min(shortestMask, lowestCommon); hadAny = true; } if (!hadAny) { throw new ArgumentException("Input was empty", nameof(others)); } return(new IpAddressNetworkV4(final, shortestMask)); }
public static void SerializeGivenChangeMask( BinaryWriter writer, NetworkedComponentTypeInfo networkedComponentTypeInfo, object value, uint changeMask ) { uint changeMaskBitIndex = 0; foreach (var field in networkedComponentTypeInfo.ThingsToSynchronize) { if (BitUtilities.GetBit(changeMask, (byte)changeMaskBitIndex)) { object fieldValue; Type fieldType; if (field.FieldInfo != null) { fieldValue = field.FieldInfo.GetValue(value); fieldType = field.FieldInfo.FieldType; } else if (field.PropertyInfo != null) { fieldValue = field.PropertyInfo.GetValue(value); fieldType = field.PropertyInfo.PropertyType; } else { throw new Exception("Invalid field to synchronize."); } SerializeObject( writer, fieldValue, fieldType, field.IsNullableIfReferenceType, field.AreElementsNullableIfReferenceType ); } changeMaskBitIndex++; } }
public CodingTableStreamer(Dictionary <int, Fraction> frequencyDictionary) { int largestNumberBitsSymbol = 0, largestNumberBitsFrequency = 0; foreach (KeyValuePair <int, Fraction> pair in frequencyDictionary) { int numBitsSymbol = BitUtilities.GetNumberBitsToRepresent(pair.Key); int numBitsFrequency = BitUtilities.GetNumberBitsToRepresent(pair.Value.Numerator); if (numBitsSymbol > largestNumberBitsSymbol) { largestNumberBitsSymbol = numBitsSymbol; } if (numBitsFrequency > largestNumberBitsFrequency) { largestNumberBitsFrequency = numBitsFrequency; } NumFrequencies++; } LargestSymbolBits = largestNumberBitsSymbol; CalculateType(); LargestFrequencyBits = largestNumberBitsFrequency; FreqDict = frequencyDictionary; }
/// <summary> /// Decode value of the immediateSwitch /// </summary> /// <param name="inputBits">our instruction</param> private void DecodeImmediateSwitch(int inputBits) { immediateSwitchValue = BitUtilities.MaskInput(inputBits, immediateSwitchStartBit, immediateSwitchEndBit); }