Get bits from an array of uint's in C++ - c++

Let's say I have an array of uint32_t, N elements.
The content of the array is only meaningful if I unpack the entire N*32 bits into small fragments.
The fragment size can be highly irregular: 1 segment of 5 bits, 2 segment of 7 bits, 1 segment of 48 bits, ... Any pattern is possible.
What is the fastest way to unpack the array?
I was thinking of using a large std::bitset<N*32>, convert to a string and the read the sub-strings; but it's kind of slow.
As another possibility, for each fragment, I was thinking to:
locate the index in the array of the starting "pointer"
locate the index in the array of the end "pointer"
locate, in the single located array element, where the starting/ending bit
do a loop and read bit by bit and merge
Working but I bet there must be something more simple.
Any idea?
Thanks

What is the the fastest way to unpack the array?
Bitwise operations.

Related

Can I use GL_PACK_SKIP_PIXELS to load only the pixels from an array of c structures

I have an audio callback which is constantly writing an array of structures. The stuct has x,y,z members but also have another members after that.
Can I somehow transfer only the pixels of this array without re-arrangeing the array into new one lacking the additional members?
I see that PixelStore() can be set so TexImage2D() misses some bytes at the end of each row but is SKIP_PIXELS what i expect or something else?
Since you're reading from this array, you want UNPACK_SKIP_PIXELS.
Pixel skip is defined in pixels, not bytes or components. If for example your XYZ data is 32 bit float, so one 'pixel' = 12 bytes, the skip factor can only be 1 = 12 bytes, 2 = 24 bytes, etc. So if your struct size isn't a nice multiple of the XYZ size, it won't work.
I suggest glVertexAttribPointer is a better match for what you want to do.

Is bitset the right container to manipulate big data then move the results into memory?

I am trying to generate a 512bit pattern where the word 0xdeadbeef keeps rotating (shifted left by one) across the 512bits, each time I want to right the data to memory.
Baiscally, 0xffffffff.......deadbeefffffffff (512 bits total). Keep shifting the deadbeef part by one and after each time write the whole pattern to memory.
Is bitset the correct container in this case? I was able to use all the needed operations (<< ^ ...etc) but I can't find a way to translate the 512bit data into 64bit long long variables to write to memory.
Bitset is not really a container, it's a representation class. Iternally it may represented by array or list of bool. Only way to export its content to array is to do it manually. It's arguably more effective to do aforementioned shifting without bitset, provided that all you need is the actual offset, which gives you address of pattern, rest of bits would be defaulted to 1.
Question is, how endianness should be handled in your application: does positiob of pattern represent lsb-msb sequence within each word, or it should be byte-wise aligned.

C++ bitmap to hold binary flags

the problem is this: I need to create a bitmap (a series of binary flags) to hold true/false information about a bunch of objects; the number of object is not known a priori so I have to allocate enough flags at runtime, possibly during the bitmap creation.
Given max_num_elements elements, my first idea was to allocate an array of ((num_elements/8)+1)*sizeof(char) bits: since a char is 8bit long it can handle 8 binary flags, so I get the minimun number of chars to hold num_elements flags, with a max memory waste of 7bits.
The real problem is for checking/setting the flags: I tought to do some bitshifting on the whole array followed by a bitwise and to get flag n like
flag_n = (flag_array>>n)&0d1
but if I understood correctly, the shifting operation won't afffect the whole array, just the first element.
How can I achieve this?
std::vector<bool> is specialised to achieve exactly this.
It is actually a problem in many cases, since access to an element returns a proxy object rather than a bool&, so it doesn't quite behave like all the other containers, but it seems like in your case it suits what you need.

How to create a very large array of unique integers?

Part of an assignment for my parallel programming class is to create a file of 313 million unique integers. I figure I'll multiply two random numbers together to get a very large range, but what is the best way to check for uniqueness?
Should I create an array and search the array each time for matches? That seems as though it will be very inefficient.
Edit - the problem is to eventually sort the list (using threading / multi cores) so having a sequential list doesn't work.
You could fill the file up sequentially - the resulting file would look like:
0 1 2 3 4 5 6 7 8 9 ... 312999999
These numbers would be very simple to generate, and would be guaranteed to be unique.
For parallel programming assignment, I think maybe you need to divide the task into small and independent pieces.
As 313M is still litter than 2G which is the maximum number a 32bit can represent.
My suggestion is that you can break the 32bit number into 16bit higher and 16bit lower. For each thread you assign a fixed higher bits and the thread generate the lower 16bits. Then you combine the two parts, thus you can keep every number generated by each thread is different.

How to store bits to a huge char array for file input/output

I want to store lots of information to a block by bits, and save it into a file.
To keep my file not so big, I want to use a small number of bits to save specified information instead of a int.
For example, I want to store Day, Hour, Minute to a file.
I only want 5 bit(day) + 5 bit(Hour) + 6 bit(Minute) = 16 bit of memory for data storage.
I cannot find a efficient way to store it in a block to put in a file.
There are some big problems in my concern:
the data length I want to store each time is not constant. It depends on the incoming information. So I cannot use a structure to store it.
there must not be any unused bit in my block, I searched for some topics that mentioned that if I store 30 bits in an int(4 byte variable), then the next 3 bit I save will automatically go into the next int. but I do not want it to happen!!
I know I can use shift right, shift left to put a number to a char, and put the char into a block, but it is inefficient.
I want a char array that I can continue putting specified bits into, and use write to put it into a file.
I think I'd just use the number of bits necessary to store the largest value you might ever need for any given piece of information. Then, Huffman encode the data as you write it (and obviously Huffman decode it as you read it). Most other approaches are likely to be less efficient, and many are likely to be more complex as well.
I haven't seen such a library. So I'm afraid you'll have to write one yourself. It won't be difficult, anyway.
And about the efficiency. This kind of operations always need bits shifting and masking, because few CPUs support directly operating into bits, especially between two machine words. The only difference is you or your compiler does the translation.