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)
|