why is a numpy view backwards?

Viewed 80
import numpy as np
data = np.array([0, 0, 0, 0, 0, 0, 0, 1], dtype=np.uint8)
data.view(np.uint64)

What I would expect is the binary would be:

0b0000000000000000000000000000000000000000000000000000000000000001

but instead the 8 bit groups are reversed.

np.array([72057594037927936], dtype=np.uint64)

which is:

0b0000000100000000000000000000000000000000000000000000000000000000

Why is that? Is there computation being done that reverses this or is this just the layout?

1 Answers

Your assumptions are correct with the how binary data is order in your array. Instead of viewing as uint64, you can view as 8 bytes (V8 dtype):

import numpy as np
np.array([0, 0, 0, 0, 0, 0, 0, 1], dtype=np.uint8).view('V8')[0]
# void(b'\x00\x00\x00\x00\x00\x00\x00\x01')

However your CPU is using little endian ordering of bytes to represent integers. This means that when viewing the bytes as uint64, you get a really big number.

You can check this as follows using the struct package:

import struct
import sys

print(sys.bybteorder)
# 'little'

# view these bytes as uint64 with little endian gives a big number
struct.unpack('<Q', b'\x00\x00\x00\x00\x00\x00\x00\x01')[0]
# 72057594037927936

# view these bytes as uint64 with big endian gives 1
struct.unpack('>Q', b'\x00\x00\x00\x00\x00\x00\x00\x01')[0]
# 1

# view these bytes as uint64 with native endian gives a big number with your CPU
struct.unpack('=Q', b'\x00\x00\x00\x00\x00\x00\x00\x01')[0]
# 72057594037927936
Related