From: Joe.Thibault@f408.n107.z1.ieee.org (Joe Thibault)
Subject: Re: How to implement a bi
Organization: FidoNet node 1:107/408 - Starcom Syste, Eatontown NJ
Date: Sun, 26 Jul 1992 16:54:00 GMT

PT>From: ptran@envmsa.eas.asu.edu (Phi-Long Tran)
PT>Date: 18 Jul 92 08:36:00 GMT
PT>Organization: Arizona State University, Tempe, AZ
PT>Message-ID: <18JUL199201363630@envmsa.eas.asu.edu>
PT>Newsgroups: comp.lang.c

PT>In article <1992Jul17.143744.12549@tamsun.tamu.edu>,
PT>tpradeep@cs.tamu.edu (Pradeep K Tapadiya) writes...

PT>>In my project I need an array of 30000 elements which have {0,1} values
PT>only.
PT>>I guesss a good way to implement this is such that each element takes just
PT>>one bit. Can someone suggest me a good struct/union implementation for
PT>>a bit array.
PT>>
PT>>Ideally I should be able to do the following operations:
PT>>
PT>>     whatever.whatever...[13467] = 1; // or 0
PT>>
PT>>     if( whatever.whatever...[i] ) {  code }
PT>>
PT>>However the following is acceptable ( worst case)
PT>>
PT>>   BitSet(Array, 13467, 1);
PT>>
PT>>   if( IsBitSet(Array, i) ) { code }
PT>>
PT>> [etc.]

PT>     C does not have "bits" available as a type that you can use.  If you
PT>are not pressed for space (13467 as an index?), use an array of char.  I
PT>believe that characters are the narrowest type you can have an arrow of.

PT>---
PT>Phi-Long Tran
PT>ptran@envmsa.eas.asu.edu
PT>Arizona State University

PT>--- AMB v6.33
PT> * Origin: zeus.ieee.org (1:107/10)
     Try the following snippet:
                typedef struct{
                        unsigned int IsBitSet;
                        struct {
                                unsigned int IsBit1Set : 1;
                                unsigned int IsBit2Set : 1;
                                unsigned int WhatsIn3And4 : 2;
                        } CompressedStuff;
                } SqueezedArray;

     Of course, to be practical, you need other related data that can be
     represented as individual bits to fill out to as close to 16 bits
     as possible, since the compiler will allocate 16 bits of memory
     (sizeof Unsigned_Int_Max) to store the data...  You have to be VERY
     careful to only assign values within range to their respective
     fields; ie.
                SqueezedArray.CompressedStuff.IsBit1Set = [ONLY 0 or 1!]
                SqueezedArray.CompressedStuff.WhatsIn3And4 = [possible
                values can be only 1,2 or 3]
     Failure to comply with assigned field sizes will result in strange
     results, which can be VERY difficult to debug! Data within the
     struct can be accessed via the BITWISE operators "&" and "|".
     Shift left "<<" and Shift right ">>" are not recommended until
     you're very comfortable with the easier bitwise minipulations.

     An example of an application where such compressed info might be
     desired would be an employee payroll database, where such
     information as tax bracket, number of dependents, Do we withhold
     Fed Tax?, State Tax?, Local Tax?, Marital Status, etc. can all be
     stored in a single data word.  The filespace savings are tremendous
     when this same information, considered individually would occupy
     132 bits instead of the 16 bits currently assigned.  Take a company
     with 10,000 employees, and figure the disk savings yourself...

  SLMR 2.1a  Triple you Hard Dive space!  cd\windows <CR> DEL *.*<CR>



--  
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
 Joe Thibault - Internet: Joe.Thibault@f408.n107.z1.ieee.org
Newsgroups: comp.lang.c