Solved

Differences in implementation

Posted on 2006-07-04
6
243 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

778 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