public static double DecodeBinary(BinaryRepresentation binaryRepresentation, DomainDefinition domainDefinition, int precision) { var d = Convert.ToInt64(binaryRepresentation.AsString(), 2); var bitsNumber = BitsNumberForDomainDimension(domainDefinition, precision); return((domainDefinition.End - domainDefinition.Start) * (d / (Pow(2, bitsNumber) - 1)) + domainDefinition.Start); }
private static IEnumerable <BinaryRepresentation> GetAlteredRepresentations(BinaryRepresentation bitRepresentation) { return(bitRepresentation.Bits.Select((bit, index) => { var alteredBit = bit == '0' ? '1' : '0'; var alteredRepresentation = new StringBuilder(bitRepresentation.AsString()); alteredRepresentation[index] = alteredBit; return BinaryRepresentation.Create(alteredRepresentation.ToString()); })); }
private DimensionSet <BinaryRepresentation> GetNeighbour(DimensionSet <BinaryRepresentation> subject) { var randomDimension = new Random().Next(0, subject.Count() - 1); var alteredRepresentation = new StringBuilder(subject.ElementAt(randomDimension).AsString()); var randomBit = new Random().Next(0, alteredRepresentation.Length); alteredRepresentation[randomBit] = alteredRepresentation[randomBit] == '0' ? '1' : '0'; var neighbour = subject.ToList(); neighbour[randomDimension] = BinaryRepresentation.Create(alteredRepresentation.ToString()); return(new DimensionSet <BinaryRepresentation>(neighbour)); }
/// <summary> /// Converts a byte array to a string according to the specified representation /// </summary> /// <param name="data"></param> /// <param name="representation">Specifies the number of bytes used to represent a single char</param> /// <returns></returns> /// <exception cref="NotSupportedException"> /// This method currently supports only <see cref="BinaryRepresentation.Base64" /> /// and <see cref="BinaryRepresentation.Hex" /> encodings /// </exception> public static string ToString(this byte[] data, BinaryRepresentation representation) { switch (representation) { case BinaryRepresentation.Base64: return(Convert.ToBase64String(data)); case BinaryRepresentation.Hex: var sBuilder = new StringBuilder(); foreach (var b in data) { sBuilder.Append(b.ToString("x2")); } return(sBuilder.ToString()); } throw new NotSupportedException("Unsupported " + nameof(BinaryRepresentation)); }
/// <summary> /// Converts a string into a byte array using the specified representation to perform conversion /// </summary> /// <param name="text">text to convert</param> /// <param name="representation">Specifies the binary representation of the input string</param> /// <exception cref="NotSupportedException"> /// This method currently supports only <see cref="BinaryRepresentation.Base64" /> /// and <see cref="BinaryRepresentation.Hex" /> encodings /// </exception> /// <returns></returns> public static byte[] ToByteArray(this string text, BinaryRepresentation representation) { byte[] bytes; switch (representation) { case BinaryRepresentation.Base64: bytes = Convert.FromBase64String(text); break; case BinaryRepresentation.Hex: bytes = FromHexString(text); break; default: throw new NotSupportedException("Unsupported " + nameof(BinaryRepresentation)); } return(bytes); }
public static DimensionSet <BinaryRepresentation> RandomDimensionalBinaryValueInDomainRange( Domain domain, DimensionDefinition dimensionDefinition, int precision) { var numbers = new List <BinaryRepresentation>(); for (var dimension = 1; dimension <= dimensionDefinition; dimension++) { var bitsNumber = BinaryHelper.BitsNumberForDomainDimension(domain.GetDefinitionForDimension(dimension), precision); var representation = new StringBuilder(); for (var i = 1; i <= bitsNumber; i++) { var random = new Random(Guid.NewGuid().GetHashCode()).NextDouble(); var bit = Round(random).ToString(); representation.Append(bit); } numbers.Add(BinaryRepresentation.Create(representation.ToString())); } return(new DimensionSet <BinaryRepresentation>(numbers)); }
static Machine() { //Ensure not already called. if (m_BitPatternSize != 0 | m_NativePointeSize != 0 | m_SystemBinaryRepresentation != BinaryRepresentation.Unknown) { return; } //No overflow anyway unchecked { #region Compilation Check //Determine how the code was compiled foreach (System.Reflection.Module module in SystemType.Assembly.Modules) { module.GetPEKind(out m_CodeType, out m_MachineType); break; } //https://msdn.microsoft.com/en-us/library/system.reflection.processorarchitecture.aspx //Verify the probe switch (AssemblyNameProcessorArchitecture) { case System.Reflection.ProcessorArchitecture.None: //An unknown or unspecified combination of processor and bits-per-word. { throw new System.InvalidOperationException("Please create an issue for your architecture to be supported."); } case System.Reflection.ProcessorArchitecture.MSIL: //Neutral with respect to processor and bits-per-word. //Should follow the X86 style case System.Reflection.ProcessorArchitecture.X86: //A 32-bit Intel processor, either native or in the Windows on Windows environment on a 64-bit platform (WOW64). { if (false == Machine.IsX86()) { throw new System.InvalidOperationException("Did not detect an x86 Machine"); } break; } case System.Reflection.ProcessorArchitecture.IA64: case System.Reflection.ProcessorArchitecture.Amd64: { if (false == Machine.IsX64()) { throw new System.InvalidOperationException("Did not detect an x64 Machine"); } break; } case System.Reflection.ProcessorArchitecture.Arm: { if (false == Machine.IsArm()) { throw new System.InvalidOperationException("Did not detect an Arm Machine"); } break; } } //Determine the size of pointers m_NativePointeSize = GetNativePointerSize(); //Write out information for tracing if there is a discrepancy System.Diagnostics.Trace.WriteLineIf(System.IntPtr.Size != m_NativePointeSize, string.Format("Did not detect the NativePointerSize correctly, Found:{0}, Expected:{1}", m_NativePointeSize, System.IntPtr.Size)); //Environment check? //http://superuser.com/questions/305901/possible-values-of-processor-architecture //Interop //http://stackoverflow.com/questions/767613/identifying-the-cpu-architecture-type-using-c-sharp/25284569#25284569 //Could detect prefer 32 bit code //http://apichange.codeplex.com/SourceControl/changeset/view/76c98b8c7311#ApiChange.Api/src/Introspection/CorFlagsReader.cs #endregion #region Check Bit Pattern Space //Caclulcate the pattern size until the value approaches 1 again (compare against 0 should be faster) while (1 >> ++m_BitPatternSize != 1 && m_BitPatternSize > 0) { ; } #endregion #region Determine BinaryRepresentation //Todo, branchless... switch ((m_SystemBinaryRepresentation = Common.Binary.Zero != (Common.Binary.One & -Common.Binary.One) ? (Common.Binary.Three & -Common.Binary.One) == Common.Binary.One ? BinaryRepresentation.SignedMagnitude : BinaryRepresentation.TwosComplement : BinaryRepresentation.OnesComplement)) { case BinaryRepresentation.TwosComplement: { if (false == IsTwosComplement()) { throw new System.InvalidOperationException("Did not correctly detect BinaryRepresentation"); } break; } case BinaryRepresentation.OnesComplement: { if (false == IsOnesComplement()) { throw new System.InvalidOperationException("Did not correctly detect BinaryRepresentation"); } break; } case BinaryRepresentation.SignedMagnitude: { if (false == IsSignedMagnitude()) { throw new System.InvalidOperationException("Did not correctly detect BinaryRepresentation"); } break; } default: { throw new System.NotSupportedException("Create an Issue for your Architecture to be supported."); } } #endregion } }