TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CAMERON CLARK
from: BOB STOUT
date: 1997-07-11 13:58:00
subject: Re: Signs of numbers

On , Cameron Clark (1:3811/350@fidonet) wrote: 
 >     The first bit of a number signifies the sign of a number so
 >     char c = (some number);
 >     negative = (c & 0x80);
 >     zero = (c & (-1));
 >     positive = !negative;
Cameron...
  Quite correct, the MS bit of any two's complement integral type represents 
the sign on a two's-complement machine. Your code, however suffers from 
several fatal flaws:
1.  It only works on two's-complement machines. The C and C++ language
    standards have been written to allow implementation on machines using
    other numeric representations.
2.  Your code only works on char's. Any other integral type (short, int, 
ong)
    will break it.
3.  Your code is guaranteed not to work on floats or doubles.
4.  Even if you restrict your functionality *only* integral type and add
    support for short's, int's, and long's, it will fail on unsigned types.
5.  Depending on your machine's architecture, it may fail on multibyte
    integral types depending on the "endianness" of your hardware.
  What I posted previously may be longer than yours, but it will work 
correctly on machines using any sort of numeric representation, on *any* 
built-in type, and with either C++ (with or without templates) or C. 
--- QM v1.00
---------------
* Origin: MicroFirm : Down to the C in chips (1:106/2000.6)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.