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

  1. Path: sparky!uunet!mcsun!fuug!demos!kiae!glas!demos!oracle.us!us.oracle.com!wkaufman
  2. From: wkaufman@us.oracle.com
  3. Newsgroups: comp.lang.c
  4. Date: 18 Jul 92 20:36 MDT
  5. Subject: Re: unsigned long to long without lossa
  6. Sender: Notesfile to Usenet Gateway <notes@glas.apc.org>
  7. Message-ID: <1992Jul18.163616.13897@oracle.us>
  8. References: <brself.711346221@hal>
  9. Nf-ID: #R:brself.711346221@hal:-680706142:1992Jul18.163616.13897@oracle.us:-1978910101:001:4816
  10. Nf-From: us.oracle.com!wkaufman    Jul 18 20:36:00 1992
  11. Lines: 115
  12.  
  13.  
  14. In article <1992Jul17.201215.19749@wyvern.twuug.com> alpha@wyvern.twuug.com (Joe Wright) writes:
  15. ] brself@hal.gnu.ai.mit.edu (Ben Self) writes:
  16. ] : 
  17. ] : Can anyone please tell me a portable (one which does not step on either
  18. ] : the ANSI or POSIX standards) way to preserve the bit order and precision
  19. ] : of an unsigned long that has to be crammed into a variable of type long.
  20.  
  21.     If (ulong_val > INT_MAX), there's not much you can do.  Someone else
  22. (name lost) suggested the following code:
  23.  
  24.         if (ulong_val > INT_MAX)
  25.             slong_val = ulong_val - INT_MAX;
  26.  
  27.     First, there may be problems if you're not doing this on a two's
  28. complement machine (OK, I don't know *what* problems--I'm just
  29. guessing).
  30.  
  31.     Second, it assumes that negative numbers are acceptable to whatever
  32. takes a Message type (which may well be true--some info from the
  33. poster on the Message type and the message codes could help).
  34.  
  35.     And third, I'm not sure this will work, because (long) isn't big
  36. enough for the left side of the minus sign, and (unsigned long) can't
  37. take the negative value of the whole, so I don't know if the machine
  38. could find an suitable intermediate value for the calculation.  (If I'm
  39. wrong, someone will be along in a bit to correct me, in a violent
  40. manner.)
  41.  
  42. ] : I know that ANSI treats unsigned long as wider type than long and if
  43. ] : mixed all values are promoted to unsigned long.
  44.  
  45.     In real life (as opposed to ANSI), some machines will have the same
  46. top value for both--signed-bit machines, for example.  But, for the
  47. majority of machines you'll use, unsigned will be one bit longer.  (I
  48. almost wrote, "...a bit longer", which isn't quite the same thing.
  49. Amazing how technical terms can mangle the language.)
  50.  
  51. ] : I know they both have the same size; surely there must be a way to
  52. ] : assure there is no loss of information in a portable way.
  53.  
  54.     If Message is your own invention (not buried in some vendor's
  55. header, for example), and you can't change the numbers, you might want
  56. to just add a bit to the type, like
  57.  
  58.         if (ulong_val > INT_MAX)
  59.           {
  60.             message.long_val = ulong_val - INT_MAX;
  61.             message.big_number = 1;
  62.           }
  63.         else
  64.           {
  65.             message.long_val = ulong_val;
  66.             message.big_number = 0;
  67.           }
  68.  
  69. and reconstruct the ulong_val the other way.
  70.  
  71.     If, on the other hand, Message *isn't* your own invention, but the
  72. message codes *are*, lower the numbers--assuming you're on a 32-bit
  73. machine, you don't really have two billion messages, right?  Or, even if
  74. you do, you could use negative numbers, right?
  75.  
  76.     If you don't own either the message codes *or* the Message type,
  77. well,...
  78.  
  79. ] Most of my posts generate flames from 'knowledgable' readers and
  80. ] alternatives to any suggestions.  So here goes..
  81.  
  82.     I *swear*, this is *not* a flame!
  83.  
  84. ] Historically, 'unsigned long' has been supported by the language
  85. ] but not by the implementation.
  86.  
  87.     Actually, if the hardware supports unsigned types, it's likely that
  88. the compiler will, too.  But, yeah, the above may be true for PCs, and
  89. many of the RISC machines out now,...
  90.  
  91. ]  This has to do with the complexity
  92. ] (in the implementation) of treating the two differently.  Really, 
  93. ] why should we treat 'unsigned long'?  Nobody needs an integer bigger
  94. ] that 2 billion, surely.  
  95.  
  96.     ...if you're lucky enough to be on a 32-plus-bit machine.  In the
  97. original poster's case, 2 billion is probably good enough for the number
  98. of possible messages--who'd even want to type in 2 billion messages?!?
  99. But for calculating the US national debt, for example, it's not enough.
  100. (Well, OK, 4 billion isn't large enough either,...)
  101.  
  102.     (As a random anecdote, a friend was working for AT&T during the time
  103. of The Great Breakup, writing software to figure out what money went
  104. where.  Early on, he wasn't sure whether to display values in millions
  105. or billions,... When you get that far up there, every bit helps.)
  106.  
  107. ] If the ANSI standard provides different sizes for 'long int' and 
  108. ] 'unsigned long int' it is news to me.  I would guess that all
  109. ] implementations will have them the same size in any case.
  110.  
  111.     On a two's-complement machine, half the space is taken up with
  112. negative numbers, right?  If you drop those (i.e., if you use a real
  113. unsigned integer type), you'll increase the top value.  On this Sun
  114. machine, for instance, limits.h lists
  115.  
  116.         #define    INT_MAX             0x7FFFFFFF
  117.         #define    UINT_MAX         0xFFFFFFFF
  118.  
  119.     This isn't a wackiness of the C implementation, it's a fact of the
  120. two's-complement integer space.
  121.  
  122.                                            -- Bill K.
  123.  
  124. Bill Kaufman,          | "...all conscious species are plastic and
  125. Corporate Lackey       |  all plastic species are conscious."
  126. wkaufman@us.oracle.com |                     -- Yew-Kwang Ng
  127.  
  128.