473,320 Members | 1,814 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 off an unsiged long

Hi,

I am reading a file that contains a bunch of unsigned longs.
The number itself is broken down in bits, for example

bit 31-24 is a certain code and bit 7-6 represents a certain state.

am I right in doing the following

// read the unsigned long from the file
unsigned long ulDataFromFile = 0;
/*
... read it
*/
// define some values, code and state
unsigned short usCode = 0; // size = 7 'cause we are reading bit 31-24
unsigned char ucState = 0; // size = 1 'cause we are reading bit 7-6

// use the unsigned long to read the data itself.
memcpy( &usCode, ((unsigned char*)ulDataFromFile)+24, sizeof(unsigned
char) ); // read bit 31-24
memcpy( &ucState , ((unsigned char*)ulDataFromFile)+7, sizeof(unsigned
short ) ); // read bit 7-6

Am I right?

I cannot really test the data as I am not sure what values to expect inside
the unsigned long

Many thanks in advance.

Simon
Jul 23 '05 #1
4 5493
Simon schrieb:
Hi,

I am reading a file that contains a bunch of unsigned longs.
The number itself is broken down in bits, for example

bit 31-24 is a certain code and bit 7-6 represents a certain state.

am I right in doing the following

// read the unsigned long from the file
unsigned long ulDataFromFile = 0;
/*
... read it
*/
// define some values, code and state
unsigned short usCode = 0; // size = 7 'cause we are reading bit 31-24
unsigned char ucState = 0; // size = 1 'cause we are reading bit 7-6 The actual sizes don't matter as long as they're large enough to hold
the value. BTW, an unsigned char is still enough to hold 7 bits...

// use the unsigned long to read the data itself.
memcpy( &usCode, ((unsigned char*)ulDataFromFile)+24, sizeof(unsigned
char) ); // read bit 31-24 No, this reads a byte from 24 *bytes* after the start of ulDataFromFile:
undefined behavious
memcpy( &ucState , ((unsigned char*)ulDataFromFile)+7, sizeof(unsigned
short ) ); // read bit 7-6 Similarly here.
Am I right? I'm afraid you aren't.
// right shift by 24 bits to discard bits 0-23
// then mask out anything beyond bit 31 (which is now bit 7)
usCode = ( ulDataFromFile >> 24 ) & 0xff;
// right shift by 6 bits to discard bits 0-5
// then mask out anything beyond bit 7 (which is now bit 1)
ucState = ( ulDataFromFile >> 6 ) & 0x03;

I cannot really test the data as I am not sure what values to expect inside
the unsigned long

You can easily try by setting ulDataFromFile to a made-up value and
compare the results to what you'd expect...
e.g. if ulDataFromFile is 0x12345678, usCode must afterwards be 0x12 and
ucState must be 0x01

HTH,
Malte
Jul 23 '05 #2
"Simon" <sp********@myoddweb.com> wrote in message
news:3a*************@individual.net
Hi,

I am reading a file that contains a bunch of unsigned longs.
The number itself is broken down in bits, for example

bit 31-24 is a certain code and bit 7-6 represents a certain state.

am I right in doing the following

// read the unsigned long from the file
unsigned long ulDataFromFile = 0;
/*
... read it
*/
// define some values, code and state
unsigned short usCode = 0; // size = 7 'cause we are reading
bit 31-24 unsigned char ucState = 0; // size = 1 'cause we are
reading bit 7-6

// use the unsigned long to read the data itself.
memcpy( &usCode, ((unsigned char*)ulDataFromFile)+24, sizeof(unsigned
char) ); // read bit 31-24
memcpy( &ucState , ((unsigned char*)ulDataFromFile)+7,
sizeof(unsigned short ) ); // read bit 7-6

Am I right?

I cannot really test the data as I am not sure what values to expect
inside the unsigned long

Many thanks in advance.

Simon


Try this:

#include <cmath>

class BitExtractor
{
public:
BitExtractor(unsigned long data) : databits(data)
{}

unsigned long Extract(size_t lowBit, size_t highBit) const
{
int digitCount = highBit - lowBit + 1;
unsigned long mask = (unsigned long)pow((int)2, digitCount) - 1;
return (databits>>lowBit) & mask;
}
private:
unsigned long databits;
};
Use it as follows:

BitExtractor be(ulDataFromFile);

// to get bits from index 6 to 7

unsigned long six_to_seven = be.Extract(6,7);

// to get bits from index 24 to 31

unsigned long twentyfour_to_thirtyone = be.Extract(24,31);
--
John Carson
Jul 23 '05 #3
<snip>
John Carson


Many thanks John and Malte, I guess I was confused with Bytes and bits.

Simon
Jul 23 '05 #4
Malte Starostik wrote:
Simon schrieb:

bit 31-24 is a certain code and bit 7-6 represents a
certain state.

unsigned long ulDataFromFile = 0;

memcpy( &usCode, ((unsigned char*)ulDataFromFile)+24,
sizeof(unsigned char) ); // read bit 31-24


No, this reads a byte from 24 *bytes* after the start of
ulDataFromFile: undefined behavious


That would be: ((unsigned char *)&ulDataFromFile)+24 . The OP code
is far worse than that.

Jul 23 '05 #5

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

Similar topics

6
by: Dietrich Epp | last post by:
Are there any good modules for reading a bitstream? Specifically, I have a string and I want to be able to get the next N bits as an integer. Right now I'm using struct.unpack and bit operations,...
4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
14
by: Ben | last post by:
Hi, I need to write some data types into an array of unsigned chars. These are basically "signals" within a message, so each signal will have a start bit and a length. The signals will also...
6
by: Mantorok Redgormor | last post by:
Would a portable method of handling 32 bits of data from a file, be to use something like unsigned long(I need to read in 32-bits of data)? or is there some smaller type that guarantees at least 32...
36
by: Digital Puer | last post by:
Hi, suppose I have an unsigned long long. I would like to extract the front 'n' bits of this value and convert them into an integer. For example, if I extract the first 3 bits, I would get an int...
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
7
by: gene kelley | last post by:
I have an application where I need to read some header information found in 3 different types of file types. Two of the file types were fairly straight forward as the items to read in the header...
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...
13
by: rsk | last post by:
Hi Friends, My requirement is as follows; A file is consisting of data in hexadecimal format(i.e a 32 bit data for example like "0xdeadbeef"). I have to read each of such data into my 'c'...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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

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.