bitarray

Compressing a sparse bit array

时间秒杀一切 提交于 2019-12-07 14:51:39
问题 I have arrays of 1024 bytes (8192 bits) which are mostly zero. Between 0.01% and 10% of bits will be set (random, no pattern). How could these be compressed, given the lack of structure and the relatively small size? (My first thought was to store the distances between set bits. I need 13 bits for each distance, but at worst case 10% occupancy this needs 13 * 816 / 8 = 1326 bytes, which is not an improvement.) This is for ultra-low bandwidth comms, so every byte matters. 回答1: I've dealt

Comparing arbitrary bit sequences in a byte array in c

倖福魔咒の 提交于 2019-12-07 10:20:10
问题 I have a couple uint8_t arrays in my c code, and I'd like to compare an arbitrary sequence bits from one with another. So for example, I have bitarray_1 and bitarray_2, and I'd like to compare bits 13 - 47 from bitarray_1 with bits 5-39 of bitarray_2. What is the most efficient way to do this? Currently it's a huge bottleneck in my program, since I just have a naive implementation that copies the bits into the beginning of a new temporary array, and then uses memcmp on them. 回答1: three words:

Efficient Datastructure for tags?

泪湿孤枕 提交于 2019-12-07 06:03:02
问题 Imagine you wanted to serialize and deserialize stackoverflow posts including their tags as space efficiently as possible (in binary), but also for performance when doing tag lookups. Is there a good datastructure for that kind of scenario? Stackoverflow has about 28532 different tags, you could create a table with all tags and assign them an integer, Furthermore you could sort them by frequency so that the most common tags have the lowest numbers. Still storing them simply like a string in

C# Prime Generator, Maxxing out Bit Array

最后都变了- 提交于 2019-12-06 14:33:37
问题 (C#, prime generator) Heres some code a friend and I were poking around on: public List<int> GetListToTop(int top) { top++; List<int> result = new List<int>(); BitArray primes = new BitArray(top / 2); int root = (int)Math.Sqrt(top); for (int i = 3, count = 3; i <= root; i += 2, count++) { int n = i - count; if (!primes[n]) for (int j = n + i; j < top / 2; j += i) { primes[j] = true; } } if (top >= 2) result.Add(2); for (int i = 0, count = 3; i < primes.Length; i++, count++) { if (!primes[i])

Function convert Hex String to BitArray C#

核能气质少年 提交于 2019-12-06 14:29:44
问题 I created the following function which will do as requested (convert HEX string to BitArray). I am not sure about the efficiency of the function, but my main problem now is that the Convert.ToInt64 function is endian specific . When this is ported over to alternate chipsets we will get different results (or exceptions). So can anyone think of an alternate way to do this conversion??? public BitArray convertHexToBitArray(string hexData) { string binary_values = ""; BitArray binary_array; if

BinaryReader - Reading a Single “ BIT ”?

筅森魡賤 提交于 2019-12-05 22:04:56
Case : Again trying to capture packets through my NIC, I have developed 2 Extensions to use in capturing variable number of bits public static string ReadBits ( this BinaryReader Key , int Value ) { BitArray _BitArray = new BitArray ( Value ); for ( int Loop = 0 ; Loop > Value ; Loop++ ) { /* Problem HERE ---> */ _BitArray [ Loop ] = Key . ReadBoolean ( ); } return BitConverter . ToString ( _BitArray . ToByteArray ( ) ); } public static byte [ ] ToByteArray ( this BitArray Key ) { byte [ ] Value = new byte [ ( int ) Math . Ceiling ( ( double ) Key . Length / 8 ) ]; Key . CopyTo ( Value , 0 );

Compressing a sparse bit array

醉酒当歌 提交于 2019-12-05 21:22:28
I have arrays of 1024 bytes (8192 bits) which are mostly zero. Between 0.01% and 10% of bits will be set (random, no pattern). How could these be compressed, given the lack of structure and the relatively small size? (My first thought was to store the distances between set bits. I need 13 bits for each distance, but at worst case 10% occupancy this needs 13 * 816 / 8 = 1326 bytes, which is not an improvement.) This is for ultra-low bandwidth comms, so every byte matters. I've dealt deeply with a similar problem, but my sets are much bigger (30 million possible values with between 1 and 30

Convert a byte or int to bitset

白昼怎懂夜的黑 提交于 2019-12-05 16:55:25
问题 I have the following: int num=Integer.parseInt(lineArray[0]); byte numBit= num & 0xFF; Is there any very simple way to convert numBit to a bit array? Or even better, is there a way to bypass the byte conversion of the int and go straigh from num to a bit array? Thanks 回答1: If you want a BitSet, try: final byte b = ...; final BitSet set = BitSet.valueOf(new byte[] { b }); If you want a boolean[] , static boolean[] bits(byte b) { int n = 8; final boolean[] set = new boolean[n]; while (--n >= 0)

python bitarray to and from file

大兔子大兔子 提交于 2019-12-05 11:57:13
I'm writing a large bitarray to a file using this code: import bitarray bits = bitarray.bitarray(bin='0000011111') #just an example with open('somefile.bin', 'wb') as fh: bits.tofile(fh) However, when i attempt to read this data back using: import bitarray a = bitarray.bitarray() with open('somefile.bin', 'rb') as fh: bits = a.fromfile(fh) print bits it fails with 'bits' being a NoneType. What am i doing wrong? I think "a" is what you want. a.fromfile(fh) is a method which fills a with the contents of fh: it doesn't return a bitarray. >>> import bitarray >>> bits = bitarray.bitarray(

Function convert Hex String to BitArray C#

江枫思渺然 提交于 2019-12-04 18:37:46
I created the following function which will do as requested (convert HEX string to BitArray). I am not sure about the efficiency of the function, but my main problem now is that the Convert.ToInt64 function is endian specific . When this is ported over to alternate chipsets we will get different results (or exceptions). So can anyone think of an alternate way to do this conversion??? public BitArray convertHexToBitArray(string hexData) { string binary_values = ""; BitArray binary_array; if (hexData.Length <= "FFFFFFFFFFFFFFFF".Length) // Max Int64 { binary_values = Convert.ToString(Convert