473,387 Members | 1,745 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,387 software developers and data experts.

Bit fields in structure vs MACROS - any difference?

Hi there,

is there any difference between using bit fields structure
and plain variable plus macros for dealing with bits?

Let's say I have a two bytes divided as follow

0 - 2: level
3 - 6: type
7 - 10: whatever
11 - 15: future

The two methods are

=== First Method ===
typedef struct {
uint16_t level: 3;
uint16_t type: 4;
uint16_t whatever: 4;
uint16_t future: 5;
} myvar;

myvar x;
x.level = 2;
etc.. etc..

=== Second Method ===
#define LEVEL(x) ( (x) & 0x0007 )
#define TYPE(x) ( ((x) & (0x00015 << 3)) >> 3)
#define SET_TYPE(x) & ( (x) << 3 )
// ... whatever macro I fancy need ...
uint16_t info;

info |= SET_TYPE(4);


Now beside, macros being corrected or not, is it all about personal taste
or is there some real difference between the two methods? And if there is,
which one is it?

I googled for the comparison but found nothing.

Thanks in advance!
S.
May 21 '09 #1
2 4798
JosAH
11,448 Expert 8TB
When you use bitfields the compiler has to generate the masking and shifting code; when you use macros you're doing that job. Not many (none?) processors can handle addressable bits so you're stuck with that bit fiddling one way or another.

kind regards,

Jos
May 21 '09 #2
donbock
2,426 Expert 2GB
@emitrax
Why deal with bits at all? Declare separate integral variables for each putative bit field. This will require you to use more memory for variables, but your executable will be a little smaller and a little faster because the bit shifting and masking is not needed.

On the other hand, perhaps your intention was for the bit fields to overlay some externally defined format (hardware register, communications protocol, file format, etc.). In that case structure bit fields are a very bad idea. You cannot portably predict how the bit fields will be arranged within the underlying integral type. Even doing your own explicit masking and shifting doesn't save you from all portability limitations: don't forget to compensate for endianness!

Even if you need to overlay an external format, it might be best to use separate integer variables for all fields and use custom (and possibly nonportable) pack and unpack functions to interconvert to/from the external function. The rest of your program doesn't have to know as much about the external format.
May 21 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Raj Kotaru | last post by:
Hello all, I recently came across the following segment of code that defines a C struct: typedef struct { unsigned char unused_bits:4; unsigned char wchair_state:2; } xyz;
112
by: Carsten Hansen | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
47
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant...
16
by: fuiwong | last post by:
Can i create a constructor in a data structure? does the following codes right? sample code #include <iostream.h> struct USERID{ char UserName; char Password; char LoginName; char...
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
11
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
6
by: Tarique | last post by:
I have the program illustrating use of Bit-Fields: #include<stdio.h> struct DISK_REGISTER { unsigned ready : 1; unsigned error_occured : 1; unsigned disk_spinning : 1; unsigned...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.