GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-May-2008, 01:51
sapan_shah143 sapan_shah143 is offline
New Member
 
Join Date: May 2008
Posts: 11
sapan_shah143 is on a distinguished road

need urgent help


how to pack eight different single bit field into one eight bit character byte?

single bit field is char single:1
  #2  
Old 13-May-2008, 06:54
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 117
L7Sqr has a spectacular aura about

Re: need urgent help


google: bit shift
  #3  
Old 13-May-2008, 07:11
sapan_shah143 sapan_shah143 is offline
New Member
 
Join Date: May 2008
Posts: 11
sapan_shah143 is on a distinguished road

Re: need urgent help


i got one solution

CPP / C++ / C Code:
#include <stdio.h>
 
 
struct flagtype
{
    unsigned char a: 2;
    unsigned int b: 1;
    unsigned char c : 1;
    unsigned char d : 4;
};
 
union utype {
    struct flagtype f;
    unsigned char byte;
};
 
 
int main(int argc, char* argv[])
{
    union utype u;
    int x;
    u.byte=0;
    printf("Size of u = %d\n",sizeof(u));
    u.f.a=3;
    u.f.b=0;
    u.f.c=0;
    u.f.d=15;
    printf("Byte = %d",u.byte);
    x=u.f.d;
    return 0;
}
Last edited by admin II : 14-May-2008 at 17:18. Reason: Please surround your C code with [cpp] your code [/cpp]
  #4  
Old 13-May-2008, 07:12
sapan_shah143 sapan_shah143 is offline
New Member
 
Join Date: May 2008
Posts: 11
sapan_shah143 is on a distinguished road

Re: need urgent help


in above solution it will allocate data to all members of union. is there any way to allocate to only one member out of several
  #5  
Old 13-May-2008, 10:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: need urgent help


Quote:
Originally Posted by sapan_shah143
in above solution it will allocate data to all members of union. is there any way to allocate to only one member out of several

A union is a single storage unit (block of memory). Its size is the size of its largest member.

For a bit field, the compiler allocates a storage unit large enough to hold the total number of bits. (There is no guarantee what the actual size is; it is just guaranteed to be "large enough.")

For a struct, the compiler allocates a storage unit large enough to hold all of its members.

In all cases, the compiler may actually allocate enough storage to make successive declared items be aligned in such a way that the compiler deems desirable (typically for best access speed).

Bottom line: The actual amount of storage that a compiler uses for a bit field or a union or a struct is implementation-dependent (that is, different compilers may allocate different amounts of storage).

Here's something to try: Declare all of the members of the flagtype struct to be unsigned char. This is actually not specifically allowed by the C language standard, but it works for all of the compilers that I use these days. See footnote.

If this works and you are satisfied, then we are done.

If you want a guaranteed portable method to make sure everything gets packed into a single char, then I think you are SOL (Somewhat Out of Luck), since it's implementation dependent.

If this doesn't work, and you are still interested in a (non-portable) way of doing the deed, try the following, and tell us what you get:

CPP / C++ / C Code:
    printf("sizeof(struct flagtype) = %d\n", sizeof(struct flagtype));
    printf("Size of u               = %d\n", sizeof(u));

You should also tell us what compiler you are using, since it very well might make a difference.

Regards,

Dave

Footnote: The C standard says that "A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type." So, whether a compliant compiler even allows having a bit field declared as an unsigned char is implementation-dependent. (And of course, regardless of the language of the Standard, some compilers try harder than others to be compliant.)
  #6  
Old 14-May-2008, 23:50
shibin shibin is offline
New Member
 
Join Date: May 2008
Posts: 1
shibin is on a distinguished road

Re: need urgent help


CPP / C++ / C Code:
// Well i think atleast you will have an idea how to use the bit operations 
// to achieve the desired results.
// Using 'OR' operation you can turn on any bit without effecting the status
// of other bits.
// Similarly if you use 'AND' operation on byte then you can turn off any
// desired bit without effecting the rest.
// i may not chek this forum any more but if you have any queries ....
// [email]shibinkidd@gmail.com[/email]

// NB: this is for demo only.... i strictly suggest u not to use the global variables.


#define ON  1
#define OFF 0
char cBulb1,cBulb2,cBulb3,cShutter1,cShutter2,cShuter3,cMotor1,cMotor2;
char cStatus;
void updateStatus(){

  if(cBulb1 == ON) cStatus |= 0x80;
  else cStatus  &= 0x7F;

  if(cBulb2 == ON) cStatus |= 0x40;
  else cStatus  &= 0xBF;
  
  if(cBulb3 == ON) cStatus |= 0x20;
  else cStatus  &= 0xDF;

  if(cShutter1 == ON) cStatus |= 0x10;
  else cStatus  &= 0xEF;
 
  if(cShutter2 == ON) cStatus |= 0x08;
  else cStatus  &= 0xF7;
  
  if(cShutter3 == ON) cStatus |= 0x04;
  else cStatus  &= 0xFB;

  if(cMotor1 == ON) cStatus |= 0x02;
  else cStatus  &= 0xFD;

  if(cMotor1 == ON) cStatus |= 0x01;
  else cStatus  &= 0xFE;

}

main(){


    cBulb1 =  ON;
    cBulb2 =  OFF;
    cBulb3 =  OFF;
    cShutter1 = ON;
    cShutter2 = ON;
    cShuter3  = OFF;
    cMotor1   =  ON;
    cMotor2   = ON;
    
//      cStatus bit allocation is as given below.
//MSB   7       6        5         4              3            2         1          0       LSB
//    cBulb1 cBulb2 cBulb3 cShutter1 cShutter2 cShuter3 cMotor1 cMotor2
//

   updateStatus();

   while(1){}

}
Last edited by LuciWiz : 20-May-2008 at 06:48. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Very Very Very Very Urgent Help Rosemary C++ Forum 3 18-Apr-2007 05:05
Urgent Help Required mkjuly C Programming Language 0 14-Oct-2006 08:12
[GRAPHICS] - SymbolDesignerTool - URGENT HELP PLS metan Java Forum 0 15-Mar-2006 00:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.