Solved

Differences in implementation

Posted on 2006-07-04
6
233 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

862 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now