C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 19th, 2012, 08:02 PM
KansaiRobot KansaiRobot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 18 KansaiRobot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 17 m 30 sec
Reputation Power: 0
Bitfields

Hello.
I am currently updating a project that uses bitfields heavily. To do this I am planning to use these as well. My question is if the following is correct, appropriate or a good idea:


first a field in the bitfield:


Code:
struct {
WORD somefield:1;
WORD otherfield :1;


WORD MYFIELD:2; // The 2-bit field I am going to use


} somebitfield;

Yeah, I know the standard recommends only use unsigned ints for that, but for some reason the project uses a user defined WORD.


Ok, so I have a 2-bit field so I guess it means this can assume 4 values right?


So I define something like


Code:
 enum { NOTHING=0, STATEONE, STATETWO,STATETHREE} myenum;




So, my question is , can I safely do something like


Code:


if(somebitfield.MYFIELD==NOTHING) {


}
elseif (somebitfield.MYFIELD==STATETWO){


}



or even


Code:


switch(somebitfield.MYFIELD)
  {
   case NOTHING:   


  case STATEONE:


}




any advice will be greatly appreciated


Thanks


Kansai

Reply With Quote
  #2  
Old September 20th, 2012, 02:12 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 54 m 25 sec
Reputation Power: 1774
> Ok, so I have a 2-bit field so I guess it means this can assume 4 values right?
Yes.

But since you seem to imply that WORD is a signed type, the 4 values you have are
-2 -1 0 1

For a :1 bitfield, all you have is -1 and 0

If WORD is actually unsigned, then your enum would work just fine, either as if/else or switch/case.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old September 20th, 2012, 11:44 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,360 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 55 m 30 sec
Reputation Power: 383
I'm sure you can find a compiler option that dumps all the macros so you can easily find out what WORD means. Of course, it might be a typedef, so that won't help.

try gcc -E
then search the output.

Find out what WORD is. salem reads into your post that you think WORD is a signed type. (I wouldn't have guessed that the bitfields were likewise signed! I learned something.)

I read into your post that you're clueless. So find out!
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old September 20th, 2012, 07:44 PM
KansaiRobot KansaiRobot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 18 KansaiRobot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 17 m 30 sec
Reputation Power: 0
thanks for the replies.
I already implemented the solution and it works great. I was just a little aprehensive about bitfields and enums since I seldom use them.

Anyway, yeah I checked WORD and it was unsigned so it works fine with the values of the enum.


BY THE WAY, talking about bit fields. The standard recommends to use only unsigned ints. However my legacy code uses WORD and sometimes BYTE (another user defined type)

Since bit fields are just that: bits, I always wonder what for are these types? I mean for example:

Code:
struct{
        unsigned int field1:1;
        unsigned int field2:2;
       ...


field1 is just one bit, and field2 just two bits. Then why put the "int" there???

Any answer?

Reply With Quote
  #5  
Old September 21st, 2012, 12:36 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 54 m 25 sec
Reputation Power: 1774
Some compilers use the type as a hint to choosing the underlying storage.
Eg.
Code:
#include<stdio.h>
struct foo {
    unsigned char b1:5;
    unsigned char b2:5;
    unsigned char b3:5;
    unsigned char b4:5;
    unsigned char b5:5;
};
struct bar {
    unsigned int b1:5;
    unsigned int b2:5;
    unsigned int b3:5;
    unsigned int b4:5;
    unsigned int b5:5;
};

int main(){
    printf("%lu %lu\n", sizeof(struct foo), sizeof(struct bar) );
    return 0;
}

$ gcc foo.c
$ ./a.out 
5 4

Since it can't fit two :5 fields in a byte, it has to allocate a new byte for each field.
An int on the other hand can pack many :5 fields together, so the overall storage space is less.

However, this is what the C99 standard says
Quote:
An implementation may allocate any addressable storage unit large enough to hold a bit-
field. If enough space remains, a bit-field that immediately follows another bit-field in a
structure shall be packed into adjacent bits of the same unit. If insufficient space remains,
whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is
implementation-defined. The order of allocation of bit-fields within a unit (high-order to
low-order or low-order to high-order) is implementation-defined. The alignment of the
addressable storage unit is unspecified.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Bitfields

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap