home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / mac / programm / 20167 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  1.8 KB

  1. Path: sparky!uunet!cs.utexas.edu!tamsun.tamu.edu!tamsun.tamu.edu!news
  2. From: bpb9204@tamsun.tamu.edu (Brent Burton)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: abs(-32768) = -32768 in Th C
  5. Date: 21 Dec 1992 20:12:56 -0600
  6. Organization: Texas A&M Univ., Inc.
  7. Lines: 43
  8. Message-ID: <1h5tj8INNl11@tamsun.tamu.edu>
  9. References: <Dec.21.20.22.18.1992.22767@remus.rutgers.edu>
  10. NNTP-Posting-Host: tamsun.tamu.edu
  11. Keywords: Think C
  12.  
  13. ficara@remus.rutgers.edu (ken ficara) writes:
  14. |
  15. |Corllary: when I say "printf("%d\n",INT_MIN);" I get "-32768." But
  16. |when I open up <limits.h> and look at it, it says "#define INT_MIN
  17. |(~32767)". Huh?
  18. |
  19. |Ken Ficara                                        ficara@remus.rutgers.edu
  20.  
  21. This is correct.  For example, if we have 4-bit signed integers, then the
  22. range will be from -8 to +7.  The 4th bit (MSb) is the sign bit:
  23.   0111 = 7
  24.   0110 = 6
  25.    ...
  26.   0000 = 0
  27.   1111 = -1
  28.   1110 = -2
  29.    ...
  30.   1000 = -8
  31.  
  32. To convert a negative 2's complement bitset to it's value, you first
  33. write the complement of the bits, and then add 1 to that (now treated
  34. as unsigned value).  The resulting unsigned value is the magnitude of
  35. the negative number, so -8 (1000) is treated as:
  36.   1000 = -8                         |     1110 = -2
  37.   0111 = 7  (complement of above)   |     0001 =  1 (~)
  38.  +0001      (add one)               |    +0001      (+1)
  39.  =1000 = 8  (unsigned value here).  |    =0010 =  2 
  40.  
  41. If I defined INTMIN for the above 4 bit system, I'd define it as
  42.   #define INTMIN (~7)
  43. which evaluates to (~0111) = 1000 = -8
  44. Now just add the proper number of 0's and 1's to the above example and
  45. you get the proper values for -32768.
  46.  
  47. -Brent
  48. For a number represented in 2's complement by n bits, the max will 
  49. be  (2**n)-1 and the minimum will be -(2**n).
  50.  
  51. -- 
  52. +-------------------------+
  53. | Brent Burton    N5VMG   |    
  54. | bpb9204@tamsun.tamu.edu |  
  55. +-------------------------+ 
  56.