Re: Typecasting goes the wrong way
On Jul 9, 11:55 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"Jim Langston" <tazmas...@rocketmail.com> wrote in message
[...]
Either that, or the wiki has it wrong.
At least parts of what you quote are completely wrong.
It's important to realize that endianness only has significance
when you "serialize": access a word as a stream of bytes, or a
byte as a stream of bits.
Fuller quote from the wiki:
[end quote]
Most modern computer processors agree on bit ordering "inside" individual
bytes (this was not always the case).
Most modern computers don't define bit ordering inside
individual bytes, because most modern computers can't address
individual bits. In literature, IBM mainframes still number the
bits from 1 to n, starting with the high order bit; most other
systems I've seen number from 0 to n-1, where the number is the
power of two the bit represents when the byte/word is viewed as
an integer (which has nothing to do with bit ordering, per se).
All of the serial protocols I know use little endian; they
transmit the least significant bit first. This is probably the
only case where there is universal agreement with regards to
endian-ness, however.
This means that any single-byte value will be read the same on
almost any computer one may send it to.
So will any multi-byte valule:-). (Both IP and TCP make
extensive use of four byte integer values. If computers didn't
read them the same, neither would work, and we couldn't hold
this discussion.)
In all cases, you depend on the transmission protocol, NOT the
internal representation of the hardward (which might use 9 bit,
1's complement bytes, for all you know or care).
Integers are usually stored as sequences of bytes, so that the
encoded value can be obtained by simple concatenation.
A more accurate way of stating this is that most modern machines
allow directly addressing the individual bytes in a word.
Integers are stored as a sequence of bytes only in the sense
that the byte addresses are at successive addresses.
(Physically, it's usually the opposite; memory is organized in
words---or even larger units today---, and special hardware
limits the effects of an access to a smaller part in the case of
byte accesses.)
[...]
If, in fact, "Little endian means the little end has the same
address as the whole number", then in the code I showed (which
you snipped) Bar[0] would be 01, but, in fact, it was 02. So
something is not meshing up with real life experiences.
Little endian definitly means that the address of the least
significant byte in the word is identical to the address of the
word; big endian means that the most significant byte has the
same address as the word. And I'm not sure what one would call
the order 2301 (used for longs in PDP-11 and Intel 16 bit
processors). The easiest way to see this is to do something
like:
uint32_t x = 0x03020100 ;
uint8_t const* p = reinterpret_cast< uint8_t const* >( &x ) ;
std::cout << (int)( *p ++ ) ;
std::cout << (int)( *p ++ ) ;
std::cout << (int)( *p ++ ) ;
std::cout << (int)( *p ++ ) ;
std::cout << std::endl ;
This outputs 3210 on my Sparc, and 0123 on my PC (and 2301 with
earlier versions of the Microsoft compiler, on a PC).
In practice, it's rarely if ever relevant. You interpret the
bytes on the line or in the file as they are defined, without
worrying about internal representation. If the protocol says
that the first byte in a four byte integer is the high order
byte, that's:
result |= buf[ 0 ] << 24 ;
regardless of how the hardware is actually organized. (Of
course, the real fun comes with floating point, where modern
machines aren't even in agreement with regards to what the base
should be.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34