home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: Three dimensional array.
- Message-ID: <4401@dozo.and.nl>
- Date: 24 Jan 93 13:39:26 GMT
- References: <1993Jan23.173200.24497@news.uiowa.edu>
- Organization: AND Software BV Rotterdam
- Lines: 65
-
- In article <1993Jan23.173200.24497@news.uiowa.edu> hari@blackmail.eng.uiowa.edu (Hari Vattikota) writes:
- |I have an 3 dimensional array(big one)[64000][180][2]. each element
- |of this array is 1 bit.
- |
- |How do I declare and store this array so that the memory usage is
- |minimum ? Also I should be able to read and write any of the
- |element with minimum of masking or any other bit operations.
-
- Since most hardware doesn't support an address resolution of one single
- bit, bit masking is inevitable. The following solution comes to mind:
-
- #define BPB 8 /* Number of Bits Per Byte (assume 8) */
-
- char arrayb[64000/BPB][180][2];
-
- #define array_get(i, j, k) (arrayb[i/BPB][j][k]&(1<<(i%BPB))?1:0)
- #define array_set(i, j, k) (arrayb[i/BPB][j][k]|= (1<<(i%BPB)))
- #define array_rst(i, j, k) (arrayb[i/BPB][j][k]&= ~(1<<(i%BPB)))
-
- Here the storage requirements are minimal, but unfortunately, there _is_
- some bit fiddling necesary ... On the other hand, using bit fields
- (which cannot be indexed) will be a mess. It'll look something like this:
-
- #define BPW 16 /* Number of Bits Per Word (assume 16) */
-
- typedef struct {
- unsigned int b00:1;
- unsigned int b01:1;
- unsigned int b02:1;
- unsigned int b03:1;
- unsigned int b04:1;
- unsigned int b05:1;
- unsigned int b06:1;
- unsigned int b07:1;
- unsigned int b08:1;
- unsigned int b09:1;
- unsigned int b10:1;
- unsigned int b11:1;
- unsigned int b12:1;
- unsigned int b13:1;
- unsigned int b14:1;
- unsigned int b15:1;
- } WORD;
-
- WORD barray[64000/BPW][180][2];
-
- unsigned int array_get(int, i, int, j, int, k) {
-
- WORD w = barray[i/BPW][j][k];
-
- switch (i%BPW) {
- case 0: return w.b00;
- case 1: return w.b01;
-
- /* etc. etc. */
-
- case 15: return w.b15;
- }
- }
-
- Well, you get the picture ... I prefer the bit masking method myself.
-
- kind regards,
-
- Jos aka jos@and.nl
-