From: BITX20@groups.io [mailto:BITX20@groups.io] On Behalf Of Jerry Gaffke via Groups.Io
Sent: Thursday, March 8, 2018 17:15
Subject: Re: [BITX20] Pulling Arduino data apart
Agreed, we've wasted too much time on something not at the top of our priorities here.
Anyone wishing to continue this discussion is welcome to send me a private message.
In parting, I believe that once the data is in the CPU, in this case stored as a 32 bit integer
in a register, endian-ness is not a factor. This code looks correct to me:
Here's C code for machine A to send a 32 bit integer as a sequence of four bytes in little endian order::
sendbyte(data32); sendbyte(data32>>8); sendbyte(data32>>16); sendbyte(data32>>24);
And code for machine B to receive that 32 bit integer (assumes getbyte() returnes an unsigned 8 bit integer):
data32=getbyte(); data32|=getbyte()<<8; data32|=getbyte()<<16; data32|=getbyte<<24;
This C code doesn't care if the machine it is on is big endian or little endian.
An as I understand it, Jack disagrees, here's his argument:
My comment about putting bits on the floor meant that you had to know something about
the byte order, otherwise why are you interested only in the high byte. Your code:
to send a byte works great if the data is big endian:
01010101 00000000 00000000 00000000. // Yellow is the byte of interest
However, if you don't know the byte order and it is:
00000000 00000000 00000000 01010101
Your code would throw the relevant data on the floor. Your code is only safe if you know
the order. A union is a simple way to determine that order.
On Thu, Mar 8, 2018 at 12:53 pm, Jack Purdum wrote:
We've wasted enough bandwidth on this. I think unions are a great way to learn how data are organized for a given compiler and are well-worth knowing about. Anyone who doesn't think so can easily ignore them.