Solved

Differences in implementation

Posted on 2006-07-04
6
256 Views
Last Modified: 2010-04-01
What is the difference between generating a header (4 bytes for example) - setting bits using structs..

typedef struct teststruct      {
      unsigned int field1:16;
      unsigned int field2:2;
      unsigned int field3:3;
      unsigned int field4:3;
      unsigned int field5:8;
} packed;

packed.field1 = 1;
packed.field2 = 1;
packed.field3 = 1;
packed.field4 = 1;
packed.field5 = 12;

...then writing it to a file..
----------------------------------
And this way:

unsigned int hdr1 = 0;
int f1 = 1;
int f2 = 1;
int f3 = 1;
int f4 = 1;
int f5 = 12;
hdr1 = f1 << 16;
      hdr1 += (f2<< 14) + (f3<< 11) + (f4 << 8) + f5;
0
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 375 total points
ID: 17038472
Nowadays a good optimising compiler will no doubt eradicate eny differences, but the first form is kinder to the compiler and therefore more likely to be efficient.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 17038474
Look at the generated assembler listing on your platform to see.
0
 
LVL 86

Expert Comment

by:jkr
ID: 17038766
I'd also agree that there will hardly any difference. Yet the bitfield approach is more elegant to write and read by humans and IMO easier to maintain when it is about to apply changes later (maybe even by a different person), since it's more 'slick'.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:jkr
jkr earned 125 total points
ID: 17038769
>>and IMO easier to maintain when it is about to apply changes later

Elaborating on that: Just imagine the location of one bit changes - in a bitfield, you'd just alter the declarationin one central place.
0
 

Author Comment

by:jewee
ID: 17038789
I'm just amazed by the number of people who use the second approach.  I needed to modify code (in which the author used the second approach).  I'm just going to rewrite it using your approach - the bitfield/struct approach.

Thank you!
0
 
LVL 86

Expert Comment

by:jkr
ID: 17038847
>>I'm just amazed by the number of people who use the second approach

That always depends on what is your main task. If you are writing an app that deals with a a lot of bit settings, a bitfield is the way to go. If you only need that once or twice in your code, there's no need to do so. BTW, there is also 'std::bitset': http://www.sgi.com/tech/stl/bitset.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question