offset macro for bit fields

T

Tehn Yit Chin

Does anyone know of an equivalent macro to offset() that would work
for bit fields?

eg,

struct abc
{
enable: 1;
disable: 1;
speed: 6;
};

enable_bit_position = offset_bit(struct abc, enable);


Thanks in advance for any hints.
Cheers,
 
S

santosh

Tehn said:
Does anyone know of an equivalent macro to offset() that would work
for bit fields?

eg,

struct abc
{
enable: 1;
disable: 1;
speed: 6;
};

enable_bit_position = offset_bit(struct abc, enable);

The address of bit-fields may not be taken. What is it that you actually
want to accomplish? Perhaps there may be a way other than you imagine.
 
R

Ralf Damaschke

Tehn said:
Does anyone know of an equivalent macro to offset() that would work
for bit fields?

eg,

struct abc
{
enable: 1;
disable: 1;
speed: 6;
};

All these struct-declaration entries are missing a type-qualifier,
e.g. 'unsigned int'.
enable_bit_position = offset_bit(struct abc, enable);

No, there is none. Given an 'x' of type 'struct abc' it's just
referred by e.g. 'x.enable'. If you somehow legally access an
'unsigned int' at the start of your struct (a union might do that),
there is still 6.7.2.1p10, which makes it implementation-defined:

| An implementation may allocate any addressable storage unit large
| enough to hold a bitfield. If enough space remains, a bit-field
| that immediately follows another bit-field in a structure shall be
| packed into adjacent bits of the same unit. If insufficient space
| remains, whether a bit-field that does not fit is put into the next
| unit or overlaps adjacent units is implementation-defined. The
| order of allocation of bit-fields within a unit (high-order to
| low-order or low-order to high-order) is implementation-defined.
| The alignment of the addressable storage unit is unspecified.

Ralf
 
V

vippstar

All these struct-declaration entries are missing a type-qualifier,
e.g. 'unsigned int'.
Aside from that, the order of these is not guaranteed to be the same
as *your* declaration. (Unlike normal members)
 
T

Tehn Yit Chin

The address of bit-fields may not be taken. What is it that you actually
want to accomplish? Perhaps there may be a way other than you imagine.

I am doing some investigation on ways which the bit field could be
modified.

For example, given a more complex structure such as

struct speed_tag
{
union
{
unsigned int speed;
struct speed_part
{
unsigned int left_wheel:4;
unsigned int right_wheel:4;
}
}
}

struct speed_tag my_car;

/* access the member directly */
my_car.speed_part.left_wheel = current_speed;

/* another way of accessing */
my_car.speed &= 0x0f;
my_car.speed |= current_speed << 4;

/* what I am looking for is */
my_car.speed &= 0x0f;
my_car.speed |= current_speed << offset_bit(struct speed_part, speed);


I realise that this technique is only applicable to the platform it is
targeted for and, at this level, portability is not possible.

Cheers,
 
T

Tehn Yit Chin

I am doing some investigation on ways which the bit field could be
modified.

For example, given a more complex structure such as

struct speed_tag
{
union
{
unsigned int speed;
struct speed_part
{
unsigned int left_wheel:4;
unsigned int right_wheel:4;
}
}

}

struct speed_tag my_car;

/* access the member directly */
my_car.speed_part.left_wheel = current_speed;

/* another way of accessing */
my_car.speed &= 0x0f;
my_car.speed |= current_speed << 4;

/* what I am looking for is */
my_car.speed &= 0x0f;
my_car.speed |= current_speed << offset_bit(struct speed_part, speed);

I realise that this technique is only applicable to the platform it is
targeted for and, at this level, portability is not possible.

Cheers,

FWIW, I just came across a patent on this.
http://www.freepatentsonline.com/6938241.html
 
B

Ben Bacarisse

I am doing some investigation on ways which the bit field could be
modified.

For example, given a more complex structure such as

struct speed_tag
{
union
{
unsigned int speed;
struct speed_part
{
unsigned int left_wheel:4;
unsigned int right_wheel:4;
}
}
}


struct speed_tag my_car;

/* access the member directly */
my_car.speed_part.left_wheel = current_speed;

/* another way of accessing */
my_car.speed &= 0x0f;
my_car.speed |= current_speed << 4;

/* what I am looking for is */
my_car.speed &= 0x0f;
my_car.speed |= current_speed << offset_bit(struct speed_part, speed);


I realise that this technique is only applicable to the platform it is
targeted for and, at this level, portability is not possible.

It is not a good idea to combine these into a union. You say tou
don't care about portability because you will use one platform, but
the association can change between compiler on the same platform,
between compiler versions of the same compiler and even between
compiler options with the same compiler version.

If you need named access, a bit field will do. If you need access by
position (or some other computed data) you need to use the well-known
shift and mask methods.
 
R

rahul

Does this snippet compile? speed_part declares the type. You have not
declared a member named speed_part.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top