home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11699 < prev    next >
Encoding:
Text File  |  1992-07-29  |  3.7 KB  |  92 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!wupost!sdd.hp.com!zeus.ieee.org!fsbbs!f408.n107.z1.ieee.org!Joe.Thibault
  3. From: Joe.Thibault@f408.n107.z1.ieee.org (Joe Thibault)
  4. Subject: Re: How to implement a bi
  5. Message-ID: <15326.2A767BA8@zeus.ieee.org>
  6. Sender: news
  7. Organization: FidoNet node 1:107/408 - Starcom Syste, Eatontown NJ
  8. Date: Sun, 26 Jul 1992 16:54:00 GMT
  9. Lines: 81
  10.  
  11. PT>From: ptran@envmsa.eas.asu.edu (Phi-Long Tran)
  12. PT>Date: 18 Jul 92 08:36:00 GMT
  13. PT>Organization: Arizona State University, Tempe, AZ
  14. PT>Message-ID: <18JUL199201363630@envmsa.eas.asu.edu>
  15. PT>Newsgroups: comp.lang.c
  16.  
  17. PT>In article <1992Jul17.143744.12549@tamsun.tamu.edu>,
  18. PT>tpradeep@cs.tamu.edu (Pradeep K Tapadiya) writes...
  19.  
  20. PT>>In my project I need an array of 30000 elements which have {0,1} values
  21. PT>only.
  22. PT>>I guesss a good way to implement this is such that each element takes just
  23. PT>>one bit. Can someone suggest me a good struct/union implementation for
  24. PT>>a bit array.
  25. PT>>
  26. PT>>Ideally I should be able to do the following operations:
  27. PT>>
  28. PT>>     whatever.whatever...[13467] = 1; // or 0
  29. PT>>
  30. PT>>     if( whatever.whatever...[i] ) {  code }
  31. PT>>
  32. PT>>However the following is acceptable ( worst case)
  33. PT>>
  34. PT>>   BitSet(Array, 13467, 1);
  35. PT>>
  36. PT>>   if( IsBitSet(Array, i) ) { code }
  37. PT>>
  38. PT>> [etc.]
  39.  
  40. PT>     C does not have "bits" available as a type that you can use.  If you
  41. PT>are not pressed for space (13467 as an index?), use an array of char.  I
  42. PT>believe that characters are the narrowest type you can have an arrow of.
  43.  
  44. PT>---
  45. PT>Phi-Long Tran
  46. PT>ptran@envmsa.eas.asu.edu
  47. PT>Arizona State University
  48.  
  49. PT>--- AMB v6.33
  50. PT> * Origin: zeus.ieee.org (1:107/10)
  51.      Try the following snippet:
  52.                 typedef struct{
  53.                         unsigned int IsBitSet;
  54.                         struct {
  55.                                 unsigned int IsBit1Set : 1;
  56.                                 unsigned int IsBit2Set : 1;
  57.                                 unsigned int WhatsIn3And4 : 2;
  58.                         } CompressedStuff;
  59.                 } SqueezedArray;
  60.  
  61.      Of course, to be practical, you need other related data that can be
  62.      represented as individual bits to fill out to as close to 16 bits
  63.      as possible, since the compiler will allocate 16 bits of memory
  64.      (sizeof Unsigned_Int_Max) to store the data...  You have to be VERY
  65.      careful to only assign values within range to their respective
  66.      fields; ie.
  67.                 SqueezedArray.CompressedStuff.IsBit1Set = [ONLY 0 or 1!]
  68.                 SqueezedArray.CompressedStuff.WhatsIn3And4 = [possible
  69.                 values can be only 1,2 or 3]
  70.      Failure to comply with assigned field sizes will result in strange
  71.      results, which can be VERY difficult to debug! Data within the
  72.      struct can be accessed via the BITWISE operators "&" and "|".
  73.      Shift left "<<" and Shift right ">>" are not recommended until
  74.      you're very comfortable with the easier bitwise minipulations.
  75.  
  76.      An example of an application where such compressed info might be
  77.      desired would be an employee payroll database, where such
  78.      information as tax bracket, number of dependents, Do we withhold
  79.      Fed Tax?, State Tax?, Local Tax?, Marital Status, etc. can all be
  80.      stored in a single data word.  The filespace savings are tremendous
  81.      when this same information, considered individually would occupy
  82.      132 bits instead of the 16 bits currently assigned.  Take a company
  83.      with 10,000 employees, and figure the disk savings yourself...
  84.  
  85.  ■ SLMR 2.1a ■ Triple you Hard Dive space!  cd\windows <CR> DEL *.*<CR>
  86.  
  87.  
  88.  
  89. --  
  90. =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  91.  Joe Thibault - Internet: Joe.Thibault@f408.n107.z1.ieee.org
  92.