Solved

Differences in implementation

Posted on 2006-07-04
6
222 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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 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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now