home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18202 < prev    next >
Encoding:
Text File  |  1992-12-11  |  2.4 KB  |  59 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!spool.mu.edu!sdd.hp.com!think.com!enterpoop.mit.edu!ira.uka.de!math.fu-berlin.de!news.th-darmstadt.de!rbg.informatik.th-darmstadt.de!misar
  3. From: misar@rbg.informatik.th-darmstadt.de (walter misar)
  4. Subject: Re: quick addition using char and shorts....
  5. Sender: news@news.th-darmstadt.de (The News System)
  6. Message-ID: <1992Dec11.183920@rbg.informatik.th-darmstadt.de>
  7. Date: Fri, 11 Dec 1992 17:39:20 GMT
  8. References:  <1992Dec10.154834.8349@news2.cis.umn.edu>
  9. Nntp-Posting-Host: rbhp58.rbg.informatik.th-darmstadt.de
  10. Organization: TU Darmstadt
  11. Lines: 46
  12.  
  13. In article <1992Dec10.154834.8349@news2.cis.umn.edu>, peschko@barracuda.micro.umn.edu (Edward Peschko) writes:
  14. > hello --
  15. > I am writing an application in which both time AND memory are important....
  16. > (well, memory is more important... I am working on a massively parallel 
  17. > environment in which each processor has only 64K). Hence, as much as possible
  18. > I will be using shorts and characters for calculations..
  19. >     Is there any way to take advantage of the shorts and characters having
  20. > only 8 and 16 bits respectively to do addition? (C, unfortunately, converts
  21. > them to int types before doing the actual addition.) I was thinking of doing
  22. > a function call, but would not the function call overhead be too much to make 
  23. > this worthwhile?
  24.  
  25. But if you store the result in chars or shorts, the expanding takes place in
  26. registers only, and don't waste memory. Furthermore most compilers would
  27. detect that to chars are added to form a new char, and omit the expansion when
  28. optimizing is done.
  29.  
  30. >     Also, I was having trouble doing the bit assignments (to substitute for 
  31. > Boolean) I noticed that the only way one can do this is to put it inside a 
  32. > structure.. However, when I do a sizeof (Boolean) with my newly defined type,
  33. > I get 4 (as in 4 bytes)...
  34.  
  35. Since the machine doesn't handle single bits, it will store the one bit actually
  36. in a word or longword ( or whatever the 'natural' datasize is on your machine).
  37. But you could pack several booleans.
  38. Either:
  39. typedef struct { unsigned b0  :1;
  40.           ....
  41.          unsigned b31 :1;
  42.            } many_bools;
  43. or:
  44. typedef int manybools;
  45. #define b0 1<<0
  46. ....
  47. #define b31 1<<31
  48.  
  49. and then selecting the bits with &(~bn) and storing them with |bn .
  50.  
  51. If you want an actual boolean type, a typedef char boolean is perhaps
  52. sufficiently optimized.
  53.  
  54. -- 
  55. Walter Misar
  56. misar@rbhp56.rbg.informatik.th-darmstadt.de
  57.