bitarray

Fastest way to calculate primes in C#?

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 ...

Is there any simple way to concatenate two BitArray (C# .NET) ?

I have var previous = new BitArray(new bool[]{true}); var current = new BitArray(new bool[]{false}); I want to concatenate them. I have already tried: var next = new BitArray(previous.Count + current.Count); var index = 0; for(;index < previous.Count; index++) next[index] = previous[index]; var j = 0; for(;index < next.Count; index+...

Convert from BitArray to Byte

I have a BitArray with the length of 8, and I need a function to convert it to a byte. How to do it? Specifically, I need a correct function of ConvertToByte BitArray bit=new BitArray(new bool[] { false,false,false,false, f...

Is there something wrong with BitArrays in C#?

When I conpile this code: BitArray bits = new BitArray(3); bits[0] = true; bits[1] = true; bits[2] = true; BitArray moreBits = new BitArray(3); bits[0] = true; bits[1] = true; bits[2] = true; BitArray xorBits = bits.Xor(moreBits); foreach (bool bit in xorBits) { Console.WriteLine(bit); } I get the following output: True ...

Converting a range into a bit array

I'm writing a time-critical piece of code in C# that requires me to convert two unsigned integers that define an inclusive range into a bit field. Ex: uint x1 = 3; uint x2 = 9; //defines the range [3-9] // 98 7654 3 //must be converted to: 0000 0011 1111 1000 It may help to visualize the bits in ...

Installing bitarray in Python 2.6 on Windows

I would like to install bitarray in Windows running python 2.6. I have mingw32 installed, and I have C:\Python26\Lib\distutils\distutils.cfg set to: [build] compiler = mingw32 If I type, in a cmd.exe window: C:\Documents and Settings\john\My Documents\bitarray-0.3.5>python setup.py install I get: [normal python messages skipped] ...

C# Prime Generator, Maxxing out Bit Array

(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 ...

Does enumerating a BitArray cause lots of boxing/unboxing?

System.BitArray only implements the non-generic IEnumerable, which returns an Object for the IEnumerator.Current property. Does running a foreach over a BitArray - eg foreach (bool b in bitArray) { // ... } box and unbox each and every bit value? Looking at the bitarray enumerator in reflector, it looks like it does a fresh bitma...

Is there a generic (type-safe) BitArray in .NET?

Is there a generic BitArray in .NET? I only found the non-generic one. Can there be a generic BitArray? (i.e. would it be reasonable?) Edit: Maybe I should have said type-safe not generic. Basically when you enumerate the type as object, should it not be int or bool? Or one of them provided in another member enumerator? Example:...

What are some common uses for bitarrays?

I've done an example using bitarrays from a newbie manual. I want to know what they can be used for and what some common data structures for them (assuming that "array" is fairly loose terminology.) Thanks. ...

Optimizing bit array accesses

Hi all, I'm using Dipperstein's bitarray.cpp class to work on bi-level (black and white) images where the image data is natively stored as simply as one pixel one bit. I need to iterate through each and every bit, on the order of 4--9 megapixels per image, over hundreds of images, using a for loop, something like: for( int i = 0; i < i...

Comparing arbitrary bit sequences in a byte array in c

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 pro...

Very Compact Bitarray in Java

I'm looking for a very compact way of storing a dense variable length bitarray in Java. Right now, I'm using BitSet, but it seems to use on average 1.5*n bits of storage space for a bit vector of size n. Typically, this isn't a problem, but in this case the bitarrays being stored are a pretty significant part the memory footprint of the ...

How do I represent and work with n-bit vectors in Python?

In an assignment I am currently working on we need to work with bit vectors, but I am very unsure of how to do this in Python. They should be able to be from 4 bits to 20 bits. I have never worked with bit vector before, but I guess that one would one create arrays of unsigned bytes that you manipulated using the usual AND/OR/XOR operati...

Bit Array Equality

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 bitwi...

Can I serialize a BitArray to XML?

Hello 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.C...

How to define and work with an array of bits in C?

I want to create a very large array on which I write '0's and '1's. I'm trying to simulate a physical process called random sequential adsorption, where units of length 2, dimers, are deposited onto an n-dimensional lattice at a random location, without overlapping each other. The process stops when there is no more room left on the latt...

If I have an array of keys M, and an array of targets N, how can I verify that M[i] exists in N before searching it?

Like the title says, I'm trying to find elements of M that exist in the large constant array N. Most of the time, no element of M will exist in N, so the vast majority of searches done on M are a waste of time. I'm looking for some way to create an index to check before doing a full-scale search of M. A project similar to mine creates a...

Generating a good hash code (GetHashCode) for a BitArray

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...

How to covert a sbyte[] to BitArray? C#.Net

I'm trying to integrate two systems that deal with images. One system provides an image as a sbyte[] and the other uses a BitArray. I need to take the data from the sbyte[] and convert it into a BitArray. Anyone know how to do this? Thanks, Paul ...