home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20140 < prev    next >
Encoding:
Internet Message Format  |  1993-01-24  |  2.0 KB

  1. Path: sparky!uunet!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Three dimensional array.
  5. Message-ID: <4401@dozo.and.nl>
  6. Date: 24 Jan 93 13:39:26 GMT
  7. References: <1993Jan23.173200.24497@news.uiowa.edu>
  8. Organization: AND Software BV Rotterdam
  9. Lines: 65
  10.  
  11. In article <1993Jan23.173200.24497@news.uiowa.edu> hari@blackmail.eng.uiowa.edu (Hari Vattikota) writes:
  12. |I have an 3 dimensional array(big one)[64000][180][2]. each element
  13. |of this array is 1 bit.
  14. |
  15. |How do I declare and store this array so that the memory usage is
  16. |minimum ? Also I should be able to read and write any of the 
  17. |element with minimum of masking or any other bit operations.
  18.  
  19. Since most hardware doesn't support an address resolution of one single
  20. bit, bit masking is inevitable. The following solution comes to mind:
  21.  
  22. #define BPB 8            /* Number of Bits Per Byte (assume 8) */
  23.  
  24. char arrayb[64000/BPB][180][2];
  25.  
  26. #define array_get(i, j, k)    (arrayb[i/BPB][j][k]&(1<<(i%BPB))?1:0)
  27. #define array_set(i, j, k)     (arrayb[i/BPB][j][k]|= (1<<(i%BPB)))
  28. #define array_rst(i, j, k)    (arrayb[i/BPB][j][k]&= ~(1<<(i%BPB)))
  29.  
  30. Here the storage requirements are minimal, but unfortunately, there _is_
  31. some bit fiddling necesary ... On the other hand, using bit fields
  32. (which cannot be indexed) will be a mess. It'll look something like this:
  33.  
  34. #define BPW 16            /* Number of Bits Per Word (assume 16) */
  35.  
  36. typedef struct {
  37.     unsigned int b00:1;
  38.     unsigned int b01:1;
  39.     unsigned int b02:1;
  40.     unsigned int b03:1;
  41.     unsigned int b04:1;
  42.     unsigned int b05:1;
  43.     unsigned int b06:1;
  44.     unsigned int b07:1;
  45.     unsigned int b08:1;
  46.     unsigned int b09:1;
  47.     unsigned int b10:1;
  48.     unsigned int b11:1;
  49.     unsigned int b12:1;
  50.     unsigned int b13:1;
  51.     unsigned int b14:1;
  52.     unsigned int b15:1;
  53. } WORD;
  54.  
  55. WORD barray[64000/BPW][180][2];
  56.  
  57. unsigned int array_get(int, i, int, j, int, k) {
  58.  
  59.     WORD w = barray[i/BPW][j][k];
  60.  
  61.     switch (i%BPW) {
  62.         case 0: return w.b00;
  63.         case 1: return w.b01;
  64.  
  65.         /* etc. etc. */
  66.  
  67.         case 15: return w.b15;
  68.     }
  69. }
  70.  
  71. Well, you get the picture ... I prefer the bit masking method myself.
  72.  
  73. kind regards,
  74.  
  75. Jos aka jos@and.nl
  76.