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.
2 4820 JosAH 11,448
Recognized Expert MVP
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
donbock 2,426
Recognized Expert Top Contributor @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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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;
|
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 between C89 and C99, I would
like to know.
I have tried with different compilers, and I see some differences.
Before I file a bug report with my C vendor, I would like to
know what the correct behavior is.
struct S
|
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 multiple arrays,etc)
I do not understand how to define the structure at run time.i.e.what
fields it will contain.
|
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 values only.
Of course it is not THAT neccessary functionality,
but it could be very useful.
greetz Emil
|
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 LoginPassword;
| |
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'
is there a way to treat fields as the member name of the struct?
|
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 type specified. This is
much similar like a macro expansion in C.
can anyone please explain advantages of one over the other ?
Thanks in advance -
|
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 write type generic functions macros?
#define CUBE(I) ( (I) * (I) * (I) )
Is there some more concise set of things where inline functions should
be used instead of macros? Multi-statement macros, for example?
|
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 write_protect : 1;
unsigned head_loaded : 1;
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |