home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!tamsun.tamu.edu!tamsun.tamu.edu!news
- From: bpb9204@tamsun.tamu.edu (Brent Burton)
- Newsgroups: comp.sys.mac.programmer
- Subject: Re: abs(-32768) = -32768 in Th C
- Date: 21 Dec 1992 20:12:56 -0600
- Organization: Texas A&M Univ., Inc.
- Lines: 43
- Message-ID: <1h5tj8INNl11@tamsun.tamu.edu>
- References: <Dec.21.20.22.18.1992.22767@remus.rutgers.edu>
- NNTP-Posting-Host: tamsun.tamu.edu
- Keywords: Think C
-
- ficara@remus.rutgers.edu (ken ficara) writes:
- |
- |Corllary: when I say "printf("%d\n",INT_MIN);" I get "-32768." But
- |when I open up <limits.h> and look at it, it says "#define INT_MIN
- |(~32767)". Huh?
- |
- |Ken Ficara ficara@remus.rutgers.edu
-
- This is correct. For example, if we have 4-bit signed integers, then the
- range will be from -8 to +7. The 4th bit (MSb) is the sign bit:
- 0111 = 7
- 0110 = 6
- ...
- 0000 = 0
- 1111 = -1
- 1110 = -2
- ...
- 1000 = -8
-
- To convert a negative 2's complement bitset to it's value, you first
- write the complement of the bits, and then add 1 to that (now treated
- as unsigned value). The resulting unsigned value is the magnitude of
- the negative number, so -8 (1000) is treated as:
- 1000 = -8 | 1110 = -2
- 0111 = 7 (complement of above) | 0001 = 1 (~)
- +0001 (add one) | +0001 (+1)
- =1000 = 8 (unsigned value here). | =0010 = 2
-
- If I defined INTMIN for the above 4 bit system, I'd define it as
- #define INTMIN (~7)
- which evaluates to (~0111) = 1000 = -8
- Now just add the proper number of 0's and 1's to the above example and
- you get the proper values for -32768.
-
- -Brent
- For a number represented in 2's complement by n bits, the max will
- be (2**n)-1 and the minimum will be -(2**n).
-
- --
- +-------------------------+
- | Brent Burton N5VMG |
- | bpb9204@tamsun.tamu.edu |
- +-------------------------+
-