bitarray

How do I implement a bit array in C / Objective C

自古美人都是妖i 提交于 2019-12-04 17:35:21
问题 iOS / Objective-C: I have a large array of boolean values. This is an inefficient way to store these values – at least eight bits are used for each element when only one is needed. How can I optimise? 回答1: see CFMutableBitVector/CFBitVector for a CFType option 回答2: Try this: #define BITOP(a,b,op) \ ((a)[(size_t)(b)/(8*sizeof *(a))] op ((size_t)1<<((size_t)(b)%(8*sizeof *(a))))) Then for any array of unsigned integer elements no larger than size_t , the BITOP macro can access the array as a

Bit Array Equality

左心房为你撑大大i 提交于 2019-12-04 07:48:51
I need something a little more than the System.Collections.BitArray class in my application. Specifically, I need the bit array: To be immutable To implement equality using value semantics I created my own struct , largely copying the internals of the BitArray implementation. (Thanks, .Net Reflector !) I don't deal everyday with bitwise operations, so I don't have the highest degree of confidence in my equality implementation. (It's passing the unit tests I am throwing at it, but I may be missing edge cases.) I have my proposed solutions as answers below. I would appreciate others' feedback

Convert a byte or int to bitset

橙三吉。 提交于 2019-12-04 03:23:46
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 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) { set[n] = (b & 0x80) != 0; b <<= 1; } return set; } or, equivalently, static boolean[] bits(final byte b)

How do I read a file without any buffering in Java?

人走茶凉 提交于 2019-12-03 22:38:30
问题 I'm working through the problems in Programming Pearls, 2nd edition, Column 1. One of the problems involves writing a program that uses only around 1 megabyte of memory to store the contents of a file as a bit array with each bit representing whether or not a 7 digit number is present in the file. Since Java is the language I'm the most familiar with, I've decided to use it even though the author seems to have had C and C++ in mind. Since I'm pretending memory is limited for the purpose of

Reverse the order of bits in a bit array

二次信任 提交于 2019-12-03 10:28:30
I have a long sequence of bits stored in an array of unsigned long integers, like this struct bit_array { int size; /* nr of bits */ unsigned long *array; /* the container that stores bits */ } I am trying to design an algorithm to reverse the order of bits in *array. Problems: size can be anything, i.e. not necessarily a multiple of 8 or 32 etc, so the first bit in the input array can end up at any position within the unsigned long in the output array; the algorithm should be platform-independent, i.e. work for any sizeof(unsigned long) . Code, pseudocode, algo description etc. -- anything

Fastest way to calculate primes in C#?

╄→гoц情女王★ 提交于 2019-12-03 09:34:26
问题 I actually have an answer to my question but it is not parallelized so I am interested in ways to improve the algorithm. Anyway it might be useful as-is for some people. int Until = 20000000; BitArray PrimeBits = new BitArray(Until, true); /* * Sieve of Eratosthenes * PrimeBits is a simple BitArray where all bit is an integer * and we mark composite numbers as false */ PrimeBits.Set(0, false); // You don't actually need this, just PrimeBits.Set(1, false); // remindig you that 2 is the

What's a time efficient algorithm to copy unaligned bit arrays?

♀尐吖头ヾ 提交于 2019-12-03 07:34:01
I've had to do this many times in the past, and I've never been satisfied with the results. Can anyone suggest a fast way of copying a contiguous bit array from source to destination where both the source and destination's may not be aligned (right shifted) on convenient processor boundaries? If both the source and destination's aren't aligned , the problem can quickly be changed into one where only either of them aren't aligned (after the first copy say). As a starting point, my code inevitably ends up looking something like the following (untested, ignore side effects this is just an off the

C# - Shifting and reversing the order of bits in a byte array

十年热恋 提交于 2019-12-02 08:09:06
I am trying to get the correct int out of an array of bytes. The bytes is read from a RFIDTag via POS for .Net. (Acctually I need 18 bits) In binary the byte array is as follows: 00001110 11011100 00000000 00011011 10000000 What I need to get out of it is: 00 00000000 11101101 (int = 237) From the original bytes that would be the following bits in reverse order: ------10 11011100 00000000 I have been looking at bitArray. Array.Reverse. And several ways of shifting bits. But I just can't wrap my head around this one. Can anyone point me in the correct direction? You can get the bits and reverse

Load a (0/1) string into a bit array

↘锁芯ラ 提交于 2019-12-01 17:26:56
What is the smartest way to load a string like "10101011101010" directly into a new bit array ? ( not a byte array ) (The bits should remain in the same order as in the list.) You can do it with LINQ: var res = new BitArray(str.Select(c => c == '1').ToArray()); You can use LINQ on this case like; var yourbitarray = new BitArray(yourstring.Select(s => s == '1').ToArray()); How about something like this: string bits = "101010101010"; byte[] bytes = bits.ToCharArray().Select(c => (byte)c == '0' ? 0 : 1).ToArray(); Might work... or byte[] bytes = bits.Select(c => (byte)c == '0' ? 0 : 1).ToArray();

Load a (0/1) string into a bit array

痞子三分冷 提交于 2019-12-01 16:53:35
问题 What is the smartest way to load a string like "10101011101010" directly into a new bit array ? ( not a byte array ) (The bits should remain in the same order as in the list.) 回答1: You can do it with LINQ: var res = new BitArray(str.Select(c => c == '1').ToArray()); 回答2: You can use LINQ on this case like; var yourbitarray = new BitArray(yourstring.Select(s => s == '1').ToArray()); 回答3: How about something like this: string bits = "101010101010"; byte[] bytes = bits.ToCharArray().Select(c =>