bitarray

Generating a good hash code (GetHashCode) for a BitArray

非 Y 不嫁゛ 提交于 2021-02-19 02:02:32
问题 I need to generate a fast hash code in GetHashCode for a BitArray. I have a Dictionary where the keys are BitArrays, and all the BitArrays are of the same length. Does anyone know of a fast way to generate a good hash from a variable number of bits, as in this scenario? UPDATE: The approach I originally took was to access the internal array of ints directly through reflection (speed is more important than encapsulation in this case), then XOR those values. The XOR approach seems to work well

Combine 2 numbers in a byte

流过昼夜 提交于 2021-02-08 15:52:36
问题 I have two numbers (going from 0-9) and I want to combine them into 1 byte. Number 1 would take bit 0-3 and Number 2 has bit 4-7. Example : I have number 3 and 4. 3 = 0011 and 4 is 0100. Result should be 0011 0100. How can I make a byte with these binary values? This is what I currently have : public Byte CombinePinDigit(int DigitA, int DigitB) { BitArray Digit1 = new BitArray(Convert.ToByte(DigitA)); BitArray Digit2 = new BitArray(Convert.ToByte(DigitB)); BitArray Combined = new BitArray(8);

How To Write A String Of Binary To File C#

China☆狼群 提交于 2020-03-22 01:50:03
问题 I Have A String Of Binary Number Like temp = "0101110011" And I Want To Save That As File this Temp Have 10 char And How Can I Save This string To file With 10 Bit Length ? void Save_Data(string temp) { bool[] BoolArray = new bool[temp.Length]; BitArray Barray = new BitArray(BoolArray.Length); char[] ch = temp.ToCharArray(); for (int i = 0; i < temp.Length; i++) { if (ch[i] == '0') { Barray[i] = false; } else { Barray[i] = true; } } Stream stream = new FileStream("D:\\test.dat", FileMode

How To Write A String Of Binary To File C#

↘锁芯ラ 提交于 2020-03-22 01:49:31
问题 I Have A String Of Binary Number Like temp = "0101110011" And I Want To Save That As File this Temp Have 10 char And How Can I Save This string To file With 10 Bit Length ? void Save_Data(string temp) { bool[] BoolArray = new bool[temp.Length]; BitArray Barray = new BitArray(BoolArray.Length); char[] ch = temp.ToCharArray(); for (int i = 0; i < temp.Length; i++) { if (ch[i] == '0') { Barray[i] = false; } else { Barray[i] = true; } } Stream stream = new FileStream("D:\\test.dat", FileMode

How To Write A String Of Binary To File C#

北战南征 提交于 2020-03-22 01:47:35
问题 I Have A String Of Binary Number Like temp = "0101110011" And I Want To Save That As File this Temp Have 10 char And How Can I Save This string To file With 10 Bit Length ? void Save_Data(string temp) { bool[] BoolArray = new bool[temp.Length]; BitArray Barray = new BitArray(BoolArray.Length); char[] ch = temp.ToCharArray(); for (int i = 0; i < temp.Length; i++) { if (ch[i] == '0') { Barray[i] = false; } else { Barray[i] = true; } } Stream stream = new FileStream("D:\\test.dat", FileMode

Can I serialize a BitArray to XML?

会有一股神秘感。 提交于 2020-01-17 03:24:05
问题 I have a business class which I need to serialize to xml. It has a BitArray property. I have decorated it with [XmlAttribute] but the serialization is failing with To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.Boolean) at all levels of their inheritance hierarchy. System.Collections.BitArray does not implement Add(System.Boolean). I am not sure whether its possible to serialize to xml? If not what would be an efficient means of

Best way to store multiple lists of true false values

南楼画角 提交于 2020-01-16 05:18:37
问题 This is just to settle a curiosity - Suppose, in my C# project, I have a list containing millions of strings, each along the following lines: "123Hi1234Howdy" "Hi1Howdy23" .... And all I need to know is, for each character in the string, if it is a digit or is it a letter. So, I was thinking the easiest way to store this would be as 0's and 1's or True / False. So, in the example above, assuming I could assign IsLetter = 1 and IsDigit = 0 , I could transform each line to: "123Hi1234Howdy" >>

BitArray to integer issue

ε祈祈猫儿з 提交于 2020-01-15 11:23:09
问题 public static int getIntegerFromBitArray(BitArray bitArray) { var result = new int[1]; bitArray.CopyTo(result, 0); return result[0]; } // Input A) 01110 // Output A) 14 // Input B) 0011 // Output B) 12 <=== ????? WHY!!! :) Can some one please explain me why my second return value is 12 instead of 3?? Please ... Thank you. 回答1: Basically it's considering the bits in the opposite order to the way you were expecting - you haven't shown how you're mapping your input binary to a BitArray , but the

Performance of small sets in Python

跟風遠走 提交于 2020-01-15 05:04:29
问题 I am looking for the most efficient way to represent small sets of integers in a given range (say 0-10) in Python. In this case, efficiency means fast construction (from an unsorted list), fast query (a couple of queries on each set), and reasonably fast construction of a sorted version (perhaps once per ten sets or so). A priori the candidates are using Python's builtin set type (fast query), using a sorted array (perhaps faster to constrct?), or using a bit-array (fast everything if I was

Converting a BitArray in to UInt32 C# [duplicate]

岁酱吖の 提交于 2020-01-15 03:21:46
问题 This question already has answers here : How can I convert BitArray to single int? (4 answers) Closed 3 years ago . im trying to convert a BitArray {0,0,0,0,0,0,0,0} into this: UInt32 0x0000 How can I do this? 回答1: Try this: new BitArray(yourArray).CopyTo(intArray, 0); 来源: https://stackoverflow.com/questions/37157550/converting-a-bitarray-in-to-uint32-c-sharp