BS> several fatal flaws:
You betcha.
BS> 1. It only works on two's-complement machines. The C and C++ language
BS> standards have been written to allow implementation on machines using
BS> other numeric representations.
The implementation of the language on a specific machine should
take care of the work for you.
BS> 2. Your code only works on char's. Any other integral type (short, int,
lo
BS> 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.
Saying "8000" bit for and integer assumes that an int is 32 bits.
This is not the case for all compilers. BC4.5 is an example that
limits data types depending upon the target type (dos/win16/win32).
BS> 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.
BS> 4. Even if you restrict your functionality *only* integral type and add
BS> support for short's, int's, and long's, it will fail on unsigned
types.
It would be stupid to want to test the sign of an unsigned variable.
BS> 5. Depending on your machine's architecture, it may fail on multibyte
BS> integral types depending on the "endianness" of your hardware.
Bit 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.
BS> What I posted previously may be longer than yours, but it will work
BS> correctly on machines using any sort of numeric representation, on *any*
BS> built-in type, and with either C++ (with or without templates) or C.
For readability, I would have used if testing for each case.
Ors and Ands are for speed.
--- GEcho 1.00
---------------
* Origin: Digital OnLine Magazine! - (409)838-8237 (1:3811/350)
|