home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11443 < prev    next >
Encoding:
Internet Message Format  |  1992-07-23  |  1.4 KB

  1. Xref: sparky comp.lang.c++:11443 comp.lang.c:11540
  2. Path: sparky!uunet!caen!sdd.hp.com!think.com!barmar
  3. From: barmar@think.com (Barry Margolin)
  4. Newsgroups: comp.lang.c++,comp.lang.c
  5. Subject: Re: A Question on masking
  6. Date: 23 Jul 1992 23:12:45 GMT
  7. Organization: Thinking Machines Corporation, Cambridge MA, USA
  8. Lines: 28
  9. Message-ID: <14neddINN7d2@early-bird.think.com>
  10. References: <1992Jul22.182925.22810@newshost.lanl.gov> <1992Jul22.183418.23446@newshost.lanl.gov> <1992Jul23.192147.191534@ua1ix.ua.edu>
  11. NNTP-Posting-Host: telecaster.think.com
  12.  
  13. In article <1992Jul23.192147.191534@ua1ix.ua.edu> sdarbha@ho12.eng.ua.edu (Subrahmanyam Darbha) writes:
  14. >I need to find out a single unique number for each of all [1-999][1-999][0-3]
  15. >numbers.
  16.  
  17. /* Combine three numbers into a unique integer.
  18.    a and b are in the range [1,999], requiring 10 bits.
  19.    c is in [0,3], requiring 2 bits.
  20.    Therefore, the result requires 22 bits, and we use the bit format
  21.    aaaaaaaaaabbbbbbbbbbcc.
  22. */
  23. unsigned long
  24. combine (unsigned int a, unsigned int b, unsigned int c) {
  25.     return (((unsigned long) a) << 12 | ((unsigned long) b) << 2 | c);
  26. }
  27.  
  28. /* Undo the above transformation */
  29. void
  30. decompose (unsigned long combined,
  31.        unsigned int *a, unsigned int *b, unsigned int *c) {
  32.     *a = combined >> 12;
  33.     *b = (combined >> 2) & 0x3FF;
  34.     *c = combined & 0x3;
  35. }
  36. -- 
  37. Barry Margolin
  38. System Manager, Thinking Machines Corp.
  39.  
  40. barmar@think.com          {uunet,harvard}!think!barmar
  41.