473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading bits at an offset

Problem: read 'nBits' (where 0< nbits <= 32) from a buffer
'buffer[1024]' at a byte position 'pos' with a *bit* offset value of
'bitOffset'. Below is the code that does that (not tested for errors),
but was wondering if there is any better way of implementing the same.
Thank you.
-KK

//Step 1 - read the rest of the bits in the first byte
std::bitset< 8 > firstByte(buffer[pos]);
for ( int i = 0; i < bitOffset ; i++ )
firstByte[i] = 0;
this->val = T (firstByte.to_ulong() << nBits);
int nBitsReadSoFar = (8 - bitOffset);

if ( nBits < nBitsReadSoFar ) //throw away extra bits
{
val >>= (nBitsReadSoFar - nBits);
nBitsReadSoFar = nBits;
return val;
}

//step 2: read integral number of bytes over which the bits are spread
int nIntegralBytes = (nBits - nBitsReadSoFar)/8;
unsigned long int acc = 0;
for ( int i = 0; i < nIntegralBytes; i++ )
{
acc |= ( buffer[pos + 1 + i] << ( (nIntegralBytes -1 - i) *8) );
nBitsReadSoFar += 8;
}
acc <<= nBits - nBitsReadSoFar;
//step 3: read rest of bits in the last byte
std::bitset< 8 > lastByte(buffer[pos + nIntegralBytes + 1]);
for ( int i = (nBits - nBitsReadSoFar); i < 8 ; i++ )
lastByte[i]=0;
lastByte >>= (8 - (nBits - nBitsReadSoFar) );

//step 4 - club all the pieces of bitsets into one
val = ((firstByte.to_ulong() << nBits)| acc ) | lastByte.to_ulong();
return val;

Nov 30 '05 #1
2 2599
pe******@gmail.com wrote:
Problem: read 'nBits' (where 0< nbits <= 32) from a buffer
'buffer[1024]' at a byte position 'pos' with a *bit* offset value of
'bitOffset'. Below is the code that does that (not tested for errors),
but was wondering if there is any better way of implementing the same.
Thank you.
-KK

//Step 1 - read the rest of the bits in the first byte
std::bitset< 8 > firstByte(buffer[pos]);
for ( int i = 0; i < bitOffset ; i++ )
firstByte[i] = 0;
this->val = T (firstByte.to_ulong() << nBits);
int nBitsReadSoFar = (8 - bitOffset);

if ( nBits < nBitsReadSoFar ) //throw away extra bits
{
val >>= (nBitsReadSoFar - nBits);
nBitsReadSoFar = nBits;
return val;
}

//step 2: read integral number of bytes over which the bits are spread
int nIntegralBytes = (nBits - nBitsReadSoFar)/8;
unsigned long int acc = 0;
for ( int i = 0; i < nIntegralBytes; i++ )
{
acc |= ( buffer[pos + 1 + i] << ( (nIntegralBytes -1 - i) *8) );
nBitsReadSoFar += 8;
}
acc <<= nBits - nBitsReadSoFar;
//step 3: read rest of bits in the last byte
std::bitset< 8 > lastByte(buffer[pos + nIntegralBytes + 1]);
for ( int i = (nBits - nBitsReadSoFar); i < 8 ; i++ )
lastByte[i]=0;
lastByte >>= (8 - (nBits - nBitsReadSoFar) );

//step 4 - club all the pieces of bitsets into one
val = ((firstByte.to_ulong() << nBits)| acc ) | lastByte.to_ulong();
return val;

the fourth line in the code should have been
val =(firstByte.to_ulong() << nBits);
instead of
this->val = T (firstByte.to_ulong() << nBits);

Nov 30 '05 #2
My colleague has found the solution for me: here I'm posting it, just
for archives
*not a tested code*
On windows platform use
__int64 acc=0;
for( int i = 0; i < 5; i++ )
{
__int64 tmp = buffer[ pos + i ] << ( (7 - i) *8 );
acc |= tmp;
}
acc <<= bitOffset;
acc >>= (64- nBits);
val = unsigned long int (acc);
for UNIX replacing __int64 with long long should help I guess

Dec 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ken | last post by:
I need to convert 32 bit Windows bitmaps to jpgs with PHP. I used the function from http://groups.google.com/groups? hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=a164f4b5.0311302128.40fb37f4%...
3
by: Vince | last post by:
Hi, I have a BYTE array and I have "mapped" every bits with a string, a type and an offest: For istance my array is 20 bytes long and I did this : first name : offset 0 (bits) length 8*10...
0
by: Lata Neti | last post by:
I am trying to retrieve images from SQL server and display them on the client’s browser. Images in the SQL server are imported from MS Access 2002 I found the following code that mentions about...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
14
by: noridotjabi | last post by:
Two questions. 1)Is there any way that I can read from an executable and then execute what I have read. EXAMPLE: text text this is more text
5
by: Oyvind Eriksen | last post by:
Hello. I need to read bits from bytes in a file. I have code that works but it's very slow. Can anybody help me? The code I have is: private bool GetBit(byte b, int pos) { return ((b &...
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
2
by: O.B. | last post by:
I've written a small operation that sets/unsets bits within an unsigned integer. Is there a better way to do this? static uint setBits(uint original, uint newValue, uint offset, unint mask) {...
11
by: itdevries | last post by:
Hi, I'm trying to convert some char data I read from a binary file (using ifstream) to a float type. I've managed to convert the int types but now I need to do the float types as well but it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.