Go Premium for a chance to win a PS4. Enter to Win


Differences in implementation

Posted on 2006-07-04
Medium Priority
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;
Question by:jewee
  • 3
  • 2
LVL 17

Accepted Solution

rstaveley earned 1500 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.
LVL 17

Expert Comment

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

Expert Comment

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'.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 86

Assisted Solution

jkr earned 500 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.

Author Comment

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!
LVL 86

Expert Comment

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

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

926 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