Re: program logic based on endianness
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Fri, 10 Aug 2007 14:26:23 +0200
sam_cit@xxxxxxxxxxx writes:
Hi Everyone,
I have a program unit which does >> and << of an integer which is of
4 bytes length. The logic of shifting and action based on the result,
assumes that the system is big-endian.
Accordingly, if i need the program to work fine in a little-endian
system. I understand that the code needs to be changed.
No. At least if you work with integers.
I wanted to know if the above holds true for bitwise and (&) and
bitwise or (|). I think, the processor should take care of the
operation a&b or a|b irrespective of the endianness of the system.
Please provide your comments.
Integers (or bits), when stored in registers, are not endian in any
way. They're just integers (or bit fields).
Endianness is a mapping between the registers and the memory. This
problem appears when the size of the registers (the number of bits
they can take) is greater than the size of the addressable memory
unit.
We use addresses of octets (8 bits), and registers of 32 bits. So we
must find a way to map these 32 bits to 4 octets in memory, given an
address.
We could do:
address[0]=(register&0xff);
address[-100]=(register>>8)&0xff;
address[7]=(register>>24)&0xff;
address[6]=(register>>16)&0xff;
but this wouldn't be too practical or meaningful...
So the main two ways to do it are:
/* little endian */
address[0]=(register&0xff);
address[1]=(register>>8)&0xff;
address[2]=(register>>24)&0xff;
address[3]=(register>>16)&0xff;
/* big endian */
address[3]=(register&0xff);
address[2]=(register>>8)&0xff;
address[1]=(register>>24)&0xff;
address[0]=(register>>16)&0xff;
But there was also this popular mapping (in PDP IIRC):
address[1]=(register&0xff);
address[0]=(register>>8)&0xff;
address[3]=(register>>24)&0xff;
address[2]=(register>>16)&0xff;
So the question is whether your result is specified in terms of
integers, or in terms of bytes in memory.
If it was specified in terms of bytes in memory, you'd be better if
you'd compute each byte independently. (eg as in the above mappings).
If it's really specified for an integer, the byte order used to store
them to memory doesn't matter.
Possibly, your memory may be used as a buffer to write to a file with
a specified byte order, then you may have to store your integers in this
specified byte order. If you need to store big-endian integers, you can
use the functions htonl (and ntohl to read them), since the network
byte order is big-endian.
--
__Pascal Bourguignon__ http://www.informatimago.com/
NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
.
- References:
- program logic based on endianness
- From: sam_cit
- program logic based on endianness
- Prev by Date: Re: program logic based on endianness
- Next by Date: Re: using PACKET_STATISTICS to get packet drop count
- Previous by thread: Re: program logic based on endianness
- Index(es):
Relevant Pages
|