On , Christopher Butler (2:257/135@fidonet) wrote:
RS> 1. It only works on two's-complement machines. The C and C++ language
RS> standards have been written to allow implementation on machines
sing
RS> other numeric representations.
> The implementation of the language on a specific machine should
> take care of the work for you.
Cameron...
Yeah, but the code in question determines whether a numer is negative or
not by testing the high bit. On a 1's complement machine (yes, there are such
beasts and they do have C compilers), it will fail.
RS> 2. Your code only works on char's. Any other integral type (short, int,
RS> long will break it.
> The pseudo-code was meant only to display how to do it for a
> specific data type. The rest would take too much explaining - not all
> C++'s have the same size for int,long int, short and the like.
It wasn't clear from the post that it was pseudo-code.
RS> 3. Your code is guaranteed not to work on floats or doubles.
> I don't know the standard encoding that C/C++ uses. I've only
> studied such encodings on a VAX/VMS machine and don't have the
> bit diagram memorized.
> Still, one bit of the float is the sign for the encoding I
> studied and I'm sure its not much different that what C++ specifies.
If the compiler in question uses IEEE standard formatting, this is true.
However, just with integer types, there's no guarantee that the internal
representation uses 2's complement. Even assuming IEEE formatting, the sign
bit isn't in either the MS- or LS-bit.
RS> 4. Even if you restrict your functionality *only* integral type and add
RS> support for short's, int's, and long's, it will fail on unsigned
RS> types.
> It would be stupid to want to test the sign of an unsigned variable.
Perhaps, but it should still be smart enough to return 0 or 1 only.
RS> 5. Depending on your machine's architecture, it may fail on multibyte
RS> integral types depending on the "endianness" of your hardware.
> Bite your tongue. I've ran the code on little and big endian
> machines - the compiler will uses the correct interpretation of
> bit possitions. It is from left to right, most to least significants.
> I've used the same code on vax,dec,alpha,pc. Knowing before hand
> the 4 of the machines on the network are big endian and I was
> running a client server on a little endian machine.
This is probably true, but it still seems to be an unsafe assumption.
--- QM v1.00
---------------
* Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
|