Questions tagged [bitarray]

A bit array (also known as bitmap, bitset, bit string, or bit vector) is an array data structure that compactly stores bits.

A bit array (also known as bitmap, bitset, bit string, or bit vector) is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level parallelism in hardware to perform operations quickly. A typical bit array stores kw bits, where w is the number of bits in the unit of storage, such as a byte or word, and k is some nonnegative integer. If w does not divide the number of bits to be stored, some space is wasted due to internal fragmentation.

280 questions
56
votes
12 answers

Does Python have a bitfield type?

I need a compact representation of an array of booleans, does Python have a builtin bitfield type or will I need to find an alternate solution?
Gordon Wrigley
  • 11,015
  • 10
  • 48
  • 62
55
votes
5 answers

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,…
Eddy
  • 6,661
  • 21
  • 58
  • 71
51
votes
9 answers

Auto increment in MongoDB to store sequence of Unique User ID

I am making a analytics system, the API call would provide a Unique User ID, but it's not in sequence and too sparse. I need to give each Unique User ID an auto increment id to mark a analytics datapoint in a bitarray/bitset. So the first user…
est
  • 11,429
  • 14
  • 70
  • 118
44
votes
9 answers

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, false, false,…
Graviton
  • 81,782
  • 146
  • 424
  • 602
37
votes
10 answers

How do I create bit array in Javascript?

What is the best way of implementing a bit array in JavaScript?
DrStrangeLove
  • 11,227
  • 16
  • 59
  • 72
32
votes
1 answer

What's the difference between Array{Bool} and BitArray in Julia and how are they related?

I was writing a function for boolean 2d arrays: function foo(A::Array{Bool,2}) ... end Evaluating and testing it with A = randbool(3,3) foo(A) returns ERROR: 'foo' has no method matching foo(::BitArray{2}) Obviously, randbool() generates a…
reschu
  • 1,095
  • 12
  • 22
27
votes
11 answers

Counting bits set in a .Net BitArray Class

I am implementing a library where I am extensively using the .Net BitArray class and need an equivalent to the Java BitSet.Cardinality() method, i.e. a method which returns the number of bits set. I was thinking of implementing it as an extension…
Sam
  • 402
  • 1
  • 4
  • 9
24
votes
4 answers

Is BitArray faster in C# for getting a bit value than a simple conjuction with bitwise shift?

1). var bitValue = (byteValue & (1 << bitNumber)) != 0; 2). using System.Collections.BitArray with a Get(int index) method What is faster? In what situations for the .NET projects BitArray could be more useful than a simple conjunction with the…
Secret
  • 2,627
  • 7
  • 32
  • 46
20
votes
7 answers

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…
oligofren
  • 20,744
  • 16
  • 93
  • 180
18
votes
4 answers

Converting C# byte to BitArray

Is there any predefined function available to convert a byte into BitArray? One way would be to inspect every bit of the byte value and then perform bitwise operation. I was wondering if there is any way which is more straightforward than this.
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
17
votes
1 answer

What is the proper way to use bit array in Rust?

I need a class with functionality equal to vector in C++. The Rust documentation tells about BitVec, but use std::collections::BitVec causes Unresolved import error during compiling. According to a pull request, BitVec has been removed. Is…
Vercetti
  • 437
  • 1
  • 6
  • 17
16
votes
6 answers

Reverse the order of bits in a bit array

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…
Eques
  • 317
  • 1
  • 3
  • 9
16
votes
2 answers

Bitarray VS bool[]

I expected to find a existing question here on SO about this but i didn't. What is the advantage of using a Bitarray when you can store your bool values in a bool[]? System.Collections.BitArray biArray = new…
Byyo
  • 2,163
  • 4
  • 21
  • 35
15
votes
3 answers

BitArray returns bits the wrong way around?

This code: BitArray bits = new BitArray(new byte[] { 7 }); foreach (bool bit in bits) { Console.WriteLine(bit ? 1 : 0); } Gives me the following output: 11100000 Shouldn't it be the other way around? Like this: 00000111 I am aware that there…
haiyyu
  • 2,194
  • 6
  • 22
  • 34
15
votes
5 answers

Convert a byte or int to bitset

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…
moesef
  • 4,641
  • 16
  • 51
  • 68
1
2 3
18 19