[Numpy-discussion] numpy endian question
Thu Apr 26 14:30:33 CDT 2007
Russell E. Owen wrote:
> In converting some code from numarray to numpy I had this:
> isBigendian = (arr.isbyteswapped() != numarray.isBigEndian)
> The only numpy version I've come up with is:
> isBigEndian = (arr.dtype.descr == '>')
> which is short but very obscure. Has anyone got a suggestion for a
> clearer test? I found lots of *almost* useful flags and methods.
How about looking at arr.dtype.byteorder?
What are you trying to test for (where a data-type is big-endian or not?).
sys.byteorder is either "little" or "big"
numpy.little_endian is True if you are on a little endian system.
arr.dtype.byteorder gives you either "=" for native or ">" for
big-endian or "<" for little-endian or "|" for it doesn't matter.
To directly answer your question, however.
On page 42 of the sample pages (online for free) of my book, the
recommended translation of numarray's arr.isbyteswapped() is (not
The translation of numarray.isBigEndian is (not numpy.little_endian)
Thus a direct translation of your code is:
isBigEndian = ((not arr.dtype.isnative) != (not numpy.little_endian))
which is equivalent to
isBigEndian = (arr.dtype.isnative != numpy.little_endian)
and because != on boolean items is equivalent to XOR this is the same as
isBigEndian = arr.dtype.isnative ^ numpy.little_endian
More information about the Numpy-discussion