home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:11443 comp.lang.c:11540
- Path: sparky!uunet!caen!sdd.hp.com!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.c++,comp.lang.c
- Subject: Re: A Question on masking
- Date: 23 Jul 1992 23:12:45 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 28
- Message-ID: <14neddINN7d2@early-bird.think.com>
- References: <1992Jul22.182925.22810@newshost.lanl.gov> <1992Jul22.183418.23446@newshost.lanl.gov> <1992Jul23.192147.191534@ua1ix.ua.edu>
- NNTP-Posting-Host: telecaster.think.com
-
- In article <1992Jul23.192147.191534@ua1ix.ua.edu> sdarbha@ho12.eng.ua.edu (Subrahmanyam Darbha) writes:
- >I need to find out a single unique number for each of all [1-999][1-999][0-3]
- >numbers.
-
- /* Combine three numbers into a unique integer.
- a and b are in the range [1,999], requiring 10 bits.
- c is in [0,3], requiring 2 bits.
- Therefore, the result requires 22 bits, and we use the bit format
- aaaaaaaaaabbbbbbbbbbcc.
- */
- unsigned long
- combine (unsigned int a, unsigned int b, unsigned int c) {
- return (((unsigned long) a) << 12 | ((unsigned long) b) << 2 | c);
- }
-
- /* Undo the above transformation */
- void
- decompose (unsigned long combined,
- unsigned int *a, unsigned int *b, unsigned int *c) {
- *a = combined >> 12;
- *b = (combined >> 2) & 0x3FF;
- *c = combined & 0x3;
- }
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-