473,473 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

offset macro for bit fields

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,
Jul 9 '08 #1
7 5899
Tehn Yit Chin wrote:
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.

Jul 9 '08 #2
On Jul 9, 6:24 pm, santosh <santosh....@gmail.comwrote:
Tehn Yit Chin wrote:
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.
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,
Jul 9 '08 #3
On Jul 10, 9:14 am, Tehn Yit Chin <tehn.yit.c...@gmail.comwrote:
On Jul 9, 6:24 pm, santosh <santosh....@gmail.comwrote:
Tehn Yit Chin wrote:
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.

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
Jul 9 '08 #4
Tehn Yit Chin wrote:
FWIW, I just came across a patent on this.
http://www.freepatentsonline.com/6938241.html
Who are they kidding?

--
Ian Collins.
Jul 9 '08 #5
Tehn Yit Chin <te***********@gmail.comwrites:
On Jul 9, 6:24 pm, santosh <santosh....@gmail.comwrote:
>Tehn Yit Chin wrote:
Does anyone know of an equivalent macro to offset() that would work
for bit fields?
<snip>
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.

--
Ben.
Jul 10 '08 #6
Ian Collins <ia******@hotmail.comwrote:
Tehn Yit Chin wrote:
FWIW, I just came across a patent on this.
http://www.freepatentsonline.com/6938241.html

Who are they kidding?
The US patent office, apparently.

Richard
Jul 10 '08 #7
On Jul 10, 4:26*am, Tehn Yit Chin <tehn.yit.c...@gmail.comwrote:

struct speed_tag
{
* *union
* *{
* * * unsigned int speed;
* * * struct speed_part
* * * {
* * * * * unsigned int left_wheel:4;
* * * * * unsigned int right_wheel:4;
* * * }
* *}
}
my_car.speed_part.left_wheel = current_speed;
Does this snippet compile? speed_part declares the type. You have not
declared a member named speed_part.

Jul 11 '08 #8

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

Similar topics

4
by: Dave | last post by:
Hello all, For purposes of understanding the language better, I tried to determine the offset of a struct member by using pointers to members (instead of something such as the offsetof macro). ...
0
by: jpodesta | last post by:
Hello- I am fairly new to MS Access and would like to use some macros in .xls in an Access Module. I have tried to do this on my own but failed to make it work. I have included the xls macros...
15
by: junky_fellow | last post by:
I am trying to find the offset of a member "mbr" inside a structure "str" as follows: offset = &(struct str *)0->mbr; But, on compilation I get the following error: cc: Error: m1.c, line 55:...
10
by: junky_fellow | last post by:
I am trying to print the offset of a particulat member in a structure, but it's not working. I am using the following expression to print the offset. &(struct my_struct *)0->member_name ...
24
by: funkyj | last post by:
PROBLEM STATEMENT: I want to calculate the byte offset of a field with a struct at compile time without instantiating an instance of the struct at runtime AND I want to do this in an ANSI standard...
5
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
9
by: John Goche | last post by:
Hello, Consider the following macro to get the memory offset of a class data member: #define OFFSET(CLASSNAME, MEMBER) ((int) (&((CLASSNAME *) 0)->MEMBER)) Given that 0 may not be the...
7
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.