Questions tagged [bitset]
A collection of bits organized as an array of bits, in which each bit can be accessed separately. For C++ bitsets, prefer the tag std-bitset
756
questions
0
votes
1
answer
19
views
BitSet only grows with true bits and not false?
Currently with Java version "11.0.3" 2019-04-16 LTS on a Linux host (but would expect 8+ compatibility):
in the context of computing a picture in order to send a binary bitmap to some ...
-2
votes
0
answers
39
views
After setting the values of bitset.get()to (1,7), the answer should be [1,3,4] but o/p is different than expected. Why?
public class BitSetGetExample3 {
public static void main(String[] args) {
BitSet bitset = new BitSet();
bitset.set(0);
bitset.set(1);
bitset.set(3);
bitset....
2
votes
3
answers
61
views
How does BitSet's set method work with bits shifting to the left?
Java's BitSet class has a method Set that sets a single bit to 1 (=true). The method source code is as follows:
public void set(int bitIndex) {
if (bitIndex < 0)
throw new ...
-1
votes
1
answer
18
views
Better style to use bitwise vs comparison in certain cases?
Just wondering if it's better style/more efficient to write code like this:
if (bitset[index] & 1)
{
//do something
}
vs:
if (bitset[index] == 1)
{
//do something
}
Thanks!
1
vote
1
answer
49
views
Roaring bitmap using more storage than normal bitset
I have a bitset which i am using to track whether an item is present or not example
b = 01100110000
it represents that 2nd and 3rd items are present and 1st and 4th item are not present.
While ...
0
votes
0
answers
81
views
how to fast set the bitset by using multiple position once
how to fast set the bitset by using multiple position, eg: the position : 1,4,5,6,9,3. the bitset is 0101111001....,whether can use SIMD instruction?
I know the simple method, eg: assume the word_t is ...
0
votes
0
answers
64
views
Error 'std::out_of_range' what(): bitset::test in C++ 98
I have taken this code in comment from Question at this link .My objective is to store the unique values stored in single variable parameter
std::bitset<270> mem;
void getValue(int parameter)
{
...
0
votes
1
answer
56
views
How to increase heap memory size for Spock Framework?
The Java program runs fine but the test fails with the message:
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.BitSet.initWords(BitSet.java:167)
at java.base/...
0
votes
1
answer
49
views
What is the space complexity of bitset in this scenario
I am doing a leetcode problem where I have to find the duplicate of an array of size [1-N] inclusive and came upon this solution:
public int findDuplicate(int[] nums) {
BitSet bit = new ...
1
vote
0
answers
49
views
Is Serialized Roaring Bitmaps compressed?
According to the definition, roaring bitmap is a "better compressed bitset". Which means it is compressed by nature. But, according to my test, the size of Dgraph's Serialized Roaring ...
3
votes
3
answers
281
views
What is the fastest way to get the lowest set bit of a C++ std::bitset?
I noticed that std::bitset does not have a function for returning or popping the lowest set bit of a bitset (nor the highest set bit for that matter). What is the fastest way to accomplish this task, ...
0
votes
1
answer
60
views
std::map<int, std::bitset<256 > > thread safety w/o mutex?
I have a
std::map<int, std::bitset<256 > > m;
After construction no new keys will be inserted and no keys will be removed. Can I safely assign the bitset in one thread while reading it in ...
0
votes
1
answer
46
views
Concatenate Bits from 3 characters, taken from different locations in the bitset
I am trying to concatenate the bits of 3 characters a, b and c into a bitset of 16 bits. The constraints are the following:
Concatenate the last 2 bits of a into newVal1
Concatenate the 8 bits of b ...
0
votes
2
answers
81
views
What is this "operator" block of code in c++ class
I'm using someone's class for bitmaps, which are ways of storing chess positions in 64-bit bitsets. I was wondering what the part with auto() operator does. Is "auto" used because it returns ...
0
votes
1
answer
18
views
Populating matrix by comparing 2 bits from 2 bitsets?
How to parse/read 2 bits at once from a bitset and process it?
I want to create a matrix that compares if 2 values match and populate the matrix with a 0 or 1.
This is what I did. This is for ...
4
votes
1
answer
54
views
BitSet.size() returns negative value. Known bug?
new BitSet(Integer.MAX_VALUE).size() reports a negative value:
import java.util.BitSet;
public class NegativeBitSetSize {
public static void main(String[] args) {
BitSet a;
a = ...
0
votes
1
answer
22
views
Algorithm : partial grid count problme
In partial grid count problem
Each point is marked with 1 or 0. In this case, the problem of finding the number of subgrid with 1 in all four corners
Each row is expressed in bitset form, and while ...
0
votes
1
answer
33
views
How I can convert a part of Hex String to Integer value in Java
I am going to implement a class in Java 11 to Convert a part of hex string like "8edb12aae312456e" to integer value. For example convert from bit 18 to 23 to integer value. I actually want a ...
1
vote
1
answer
60
views
Efficiently find ranges of consecutive non-set bits in BitSet
I'm trying to represent calendar available/unavailable slots for a particular day with each bit representing 15mins using BitSet. The bits which are set represent blocked calendar events. For getting ...
0
votes
1
answer
27
views
redis async replication of a bitset
I am using redis to store some pretty large bitsets. Redis is run in master/slave sentinel mode.
I got curious about the replication performance for very big bitsets (my bitset has a size of +-...
0
votes
1
answer
49
views
How to convert BitSet -> short array with 4 bit precision?
I want to convert the BitSet data of variable length to an array of shorts.
BitSet -> short[]
Every four bits of the bitset should be used to construct a new number.
Example data:
BitSet bits = new ...
0
votes
2
answers
92
views
How to convert short array to BitSet with 2 bit precision in Java?
I have a short array {0,2,3,1,…} which I would like to convert to a BitSet.
Expected bits in bitset: 00 10 11 01 …
Every two bit in the bitset should represent a short. (2-bit precision)
This should ...
0
votes
2
answers
178
views
Print out bitset quickly in c++
Im writing a program that outputs binary, and I got alot of it I want to output to the terminal, but this takes along time.
In other places in my program where I want to quickly output strings I use
...
1
vote
1
answer
327
views
C++ - How to left/right circular shift a bitset?
Let's say I have a std::bitset<28> called left28.
I'm looking to left circular shift left28.
After doing some searching, I came across std::rotl (C++20) but it doesn't seem to play nice with ...
1
vote
1
answer
73
views
How does the cardinality work in Java BitSet?
import java.util.*;
public class Main {
public static void main(String[] args)
{
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet();
bs1.set(0);
bs1.set(1);
bs1.set(2);
bs1.set(4);
bs2.set(0);
...
0
votes
1
answer
179
views
Struct to bits c++
I am learning C++ and I wonder if it is possible to decompose a structure object into a sequence of bits?
// The task is this! I have a structure
struct test {
// It contains an array
private:
...
2
votes
2
answers
303
views
How to reduce the float rounding error when converting it into fixed-point in C++?
I have a float variable which is incremented 0.1 in each step. I want to convert it into 16-bit fixed value where it has 5-bits fractional part. In order to do that I have the code snippet below:
#...
0
votes
1
answer
162
views
How to flip all the bits in Java BitSet while preserving its length
How can I flip all the bits in a Java BitSet, while preserving its length?
For example, given the following BitSet:
BitSet.valueOf(new byte[] { 0b100111 })
// {0, 1, 2, 5}
Is there a simple method to ...
0
votes
1
answer
75
views
Can someone explain part *(int*) and how it have any relations to the bitset?
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
//from float to bits
float f;
cin >> f;
int x = *(int*)&f;
cout << x << &...
0
votes
1
answer
70
views
how to use itertools.combinations on a bitset (python)
I have a python integer that represents a bitset. How can I use itertools.combinations (or some other method) that will take an integer/bitset as input and produces all combinations of size k without ...
4
votes
1
answer
59
views
Generating all combinations of size 1 to n (bitsets)
There is a lot of quick methods on stack overflow that generate all the combination of bitsets of size k.
Ex:
combos(4,3) = {1110,1101,1011,0111}
However, is there a quick way to calculate all combos ...
1
vote
1
answer
161
views
How can I convert ANY user defined type to an std::bitset?
What I want to achieve is a means of converting any arbitrarily sized and formatted type to an std::bitset. Like this:
#include<bitset>
#include<bit>
#include<cstdlib>
#include&...
0
votes
1
answer
135
views
Using Python bitset to find all combinations of a set of size k
I am trying to speed up my own dynamic programming solution for Travelling Salesman. My original solution memoizes with a dictionary with a python frozenset() as part of the key. However, I believe ...
1
vote
1
answer
38
views
Is it necessary to check range in bit representation C++ [duplicate]
Some data are stored in a 64 bit integer. As you can see, inside the getDrvAns function I check if the bit in the position drvIdx-1 is 0 or 1. One cannot be sure if the drvIdx will have a value in ...
0
votes
2
answers
158
views
How to check the value of a bit in C++
I would like to check the value of the 5th and the 4th bit starting from the left in this strings like this one:
value: "00001101100000000000000001000110"
value: "...
-1
votes
2
answers
167
views
How to find the 3rd bit of a number [duplicate]
I have a question with a problem in c++.
I have to create a program where I have to create one variable from Int and to cout<< on the screen "True" if the 3rd bit is 1.
My question is: ...
1
vote
0
answers
84
views
How to program std::bitset<VariableValue>
I am willing to have a bitset which has it size changing depending on the instance of my class Foo.
Each instance depends on an integer n and I want to create a bitset which has the minimal number of ...
1
vote
0
answers
49
views
Java can this BitSet.or compare be done faster?
I have a long array that I would like to be compared to itself faster than what I am currently seeing. I need to perform some operation on each element, then compare each result at least once. This is ...
6
votes
2
answers
239
views
efficient bitwise sum calculation
Is there an efficient way to calculate a bitwise sum of uint8_t buffers (assume number of buffers are <= 255, so that we can make the sum uint8)? Basically I want to know how many bits are set at ...
0
votes
0
answers
174
views
How do I initialize a bitset in array order?
A std::bitset isn't an integer, it's an array of bits. But when I initialize it with the bits in the correct order (as I would an array), it seems that all of the bits get reversed like an Endian swap....
0
votes
0
answers
56
views
Performance issues with pandas and bitset data
Im running a script that processes bitset data into a pandas data frame. Essentially it examines a bitset, makes a new column for the first bit and assigns a value to the row for that bit. It then ...
2
votes
1
answer
131
views
Is it possible to shift a Java BitSet using the logical operators?
For example I have a BitSet BitSet x = new BitSet(32); and I set bit 3 x.set(2, true); Is there anyways to do a logical shift like x << someAmount; that pads with 0's?
0
votes
2
answers
265
views
How to populate a BitSet with 0s?
I am trying to populate a size 32 BitSet with all 0s.
When I do longWord.set(i); it populates with all i values.
When I do longWord.set(i,false); it doesn't populate at all.
how can I set all 32 bits ...
0
votes
2
answers
64
views
asymmetric difference betwen two binary numbers (bitsets)
It is quick and easy to determine shared/different bits between two binary numbers by AND or XOR. Let's say we have A: 10011 and B:11001 we can get the difference.
10011 XOR 11001 = 01010 (1s are ...
1
vote
1
answer
47
views
How to form maximum number of 1s from non overlapping bitsets
Given N number of bitsets consisting of M bits, choose K bitsets such that there is no multiple 1s occurring in the same position. What is the maximum number of 1s that can be formed?
Example:
N = 5, ...
0
votes
3
answers
233
views
how to split a bitset to an array
I have am enum like below:
enum types : uint16_t
{
A = 1 << 0,
B = 1 << 1,
C = 1 << 2,
D = 1 << 3,
E = 1 << 4,
F = 1 << 5,
G = 1 <<...
0
votes
2
answers
65
views
gcc warning when casting "`unsigned` bitset" to `unsigned`; is it still safe?
For debugging purposes I did cast an "unsigned bitset" type to unsigned:
The "unsigned bitset" looks like this:
typedef struct {
unsigned f1 : 1;
unsigned f2 : 1;
...
0
votes
0
answers
124
views
How can I change the type of std::bitset<8> output to an integer without manipulating the original content?
I created an array of buffers from .bin file.
std::vector<unsigned char> buffer_list(std::istreambuf_iterator<char>(file), {});
But now I need its binary values like this:
for (int i = 0; ...
0
votes
1
answer
143
views
How can i pass pointer to bitset to a function argument?
i want pass pointer to bitset to function argument,i'm declaring bitset on heap memory:
std::bitset<size> *ptr;
ptr = new std::bitset<size>();
void function('here i want pass the pointer ')...
0
votes
1
answer
181
views
Java BitSet: finding all the true bits efficiently?
Suppose the BitSet from java.util.BitSet; is used. The objective is to quickly find all the bit values that are set to true. These values are not in order and without a particular pattern. The maximum ...