How can I convert BitArray to single int?
private int getIntFromBitArray(BitArray bitArray)
{
if (bitArray.Length > 32)
throw new ArgumentException("Argument length shall be at most 32 bits.");
int[] array = new int[1];
bitArray.CopyTo(array, 0);
return array[0];
}
Convert a BitArray to Int in C#
Just copy the bit array to an int array and take the first element.
BitArray bitArray = new BitArray(new int[] { 5 });
int[] array = new int[1];
bitArray.CopyTo(array, 0);
int result = array[0];
How to convert bitarray to an integer in python
To convert a bitarray
to its integer form you can use the struct
module:
Code:
from bitarray import bitarray
import struct
d = bitarray('0' * 30, endian='little')
d[5] = 1
print(struct.unpack("<L", d)[0])
d[6] = 1
print(struct.unpack("<L", d)[0])
Outputs:
32
96
BitArray to integer issue
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 result is treating it as 1100 rather than 0011.
The documentation isn't clear, admittedly, but it does work the way I'd expect it to: bitArray[0]
represents the least significant value, just as it usually is when discussing binary (so bit 0 is 0/1, bit 1 is 0/2, bit 2 is 0/4, bit 3 is 0/8 etc). For example:
using System;
using System.Collections;
class Program
{
static void Main(string[] args)
{
BitArray bits = new BitArray(8);
bits[0] = false;
bits[1] = true;
int[] array = new int[1];
bits.CopyTo(array, 0);
Console.WriteLine(array[0]); // Prints 2
}
}
convert from BitArray to 16-bit unsigned integer in c#
You can do it like this:
UInt16 res = 0;
for (int i = 0 ; i < 16 ; i++) {
if (bits[i]) {
res |= (UInt16)(1 << i);
}
}
This algorithm checks the 16 least significant bits one by one, and uses the bitwise OR operation to set the corresponding bit of the result.
convert bitarray to set
If I understand your question:
for (int i = 0; i < 750000; ++i) {
if (bitarray_has(bitarray, i)) {
set_of_numbers.push_back(i);
}
}
I don't believe walking bitarray
will be particularly slow, but the push_back()
can be made faster if you know how many elements will be created. Then you can use reserve()
to pre-allocate the memory.
BitArray to zeros and ones
BitArray
class is the ideal class to use in your case for bitwise operations. You probably do not want to convert BitArray
to bool[]
or any other type if you want to do boolean operations. It stores bool
values efficiently (1 bit for each) and provides you the necessary methods to do bitwise operations.
BitArray.And(BitArray other)
, BitArray.Or(BitArray other)
, BitArray.Xor(BitArray other)
are for boolean operations and BitArray.Set(int index, bool value)
, BitArray.Get(int index)
to work with individual values.
EDIT
You can manipulate values individually using any bitwise operations:
bool xorValue = bool1 ^ bool2;
bitArray.Set(index, xorValue);
You can have a collection of BitArray
's of course:
BitArray[] arrays = new BitArray[2];
...
arrays[0].And(arrays[1]); // And'ing two BitArray's
Related Topics
C# How to Wait for a Webpage to Finish Loading Before Continuing
How to Read All Files Inside Particular Folder
Directory.Getfiles of Certain Extension
Convert Any Currency String to Double
High Quality Jpeg Compression with C#
Does .Net Ftpwebrequest Support Both Implicit (Ftps) and Explicit (Ftpes)
Populate Treeview from List of File Paths in Wpf
Gui and Windows Service Communication
How to Open an Excel File in C#
Force Xmlserializer to Serialize Datetime as 'Yyyy-Mm-Dd Hh:Mm:Ss'
Asynchronous Task.Whenall with Timeout
Command to Close an Application of Console
How to Bind Crystal Report to Manually Created Dataset
How to Find the State of Numlock, Capslock and Scrolllock in .Net