<Er*********@gm ail.com> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
I am reading in 1bit data to a buffer that contains over 30000 bits and
I would like to beable to bitshift the entire buffer and AND and OR it
with other buffers of the same size. I though I could use the stl
bitset class, but I cannot find a way to load the buffer into the
bitset. I have been trying with this test program (with the size
scaled down to 1024 bits) to load a buffer into the bitset, but prints
out all zeros.
#include <iostream>
#include <bitset>
int main(){
int const size = 1024;
unsigned char* data = new unsigned char[size];
*(data+1000) = 0xEA;
std::bitset<siz e> bits(*data);
std::cout<<bits .count()<<std:: endl;
std::cout<<bits <<std::endl;
delete[] data;
return 0;
}
If I had to do this, I don't think I'd actually shift the bits until a
bit/byte value was requested.
I would create a vector of unsigned char and allocate enought memory for
3000 bytes and load the data into the vector. Shifting right or left by any
multiple of 8 is trivial. Shift right by 8, delete the last char, push 0
onto the front. Shift left by 8, delete the first element, push 0 onto the
back.
Your only concern comes in then when you actually want to read the bits
(perhaps to AND or OR them). Encapsulate the vector with a class, over ride
the >> and << to do your custom shifting (only shift by mulitples of 8, any
remainder keep in a variable). Override []. When [] is used build the byte
to return. The override of [] would be a little more difficult, but not
much.
Say, for instance, they had shifted right 12. You would delete the last
element, push a 0 onto the front. That leaves a remainder of 4 to shift
right. So if the [] method requests byte 100, you would know to build the
byte by shifting the 100 element right 4, and making the high 4 bits the low
bits of element 99.
return (( BitVector[100] >> 4 ) & 0x0F ) | (( BitVector[99] << 4 ) & 0xF0)
That is untested, but I'm fairly sure it's right. Of course the constant 4
would be in a variable, and you would do something similar if it was shifted
left, and look out for your first and last elements.
The ANDing and ORing is trivial, just do it a byte at a time (with the
returned shifted value of course) and build a new vector to return.