“This is the 18th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

The core of the Python Numpy module is the Ndarray object. Ndarray objects have the following features:

  • Ndarray requires that all element data types be of the same type and that each element occupies the same block of memory.
  • Data stored in NDARray is a contiguous chunk of storage space in the computer’s underlying hard disk memory

Ndarray consists of two parts:

  • Metdata: Strides for array type dType, array dimension nDIM, Dimension number Shape, dimension strides, etc
  • Raw data: Stores raw data

Typically, raw Data stores data in a different memory than the byte order of a computer running Python.

As a result, data storage addresses in NDARray arrays change, so this installment covers the new numPY concept of byte swapping.

1. Byte sequence pattern

  • What is byte swapping?

    It is well known that multibyte objects are stored as sequential byte sequences in all calculator memory.

    Byte sort is an object that provides a Python array interface for data in Darray memory.

    Byte sorting has storage rules that span multi-byte program objects, mainly divided into big-endian mode and small-endian mode.

  • Big-endian mode:

    • The English name of the big-endian mode is big-endian

    • The high-order byte is disposed at the low-address end and the low-order byte at the high-address end

    • The big-end pattern conforms to people’s reading habits and is arranged from low to high

  • Small end mode:

    • The English name of the small-endian mode is little-endian

    • The low byte is disposed to the low end of the memory address, and the high byte is disposed to the high end of the memory address

  • Advantages of small end mode:

    • Big-endian mode: The sign bit is fixed to the first byte, which is easy to determine positive and negative
    • Small – end mode: Cast data without adjusting the byte content.

2. Change the byte order

There are two types of byte order for numpy arrays and their underlying pre-memory relationships:

  • Arr.newbyteorder () can change the byteorder information in the array dTYPE to interpret the underlying data as a different byteorder.
  • Arr.byteswap () changes the byte order of the underlying data, leaving the DTYPE interpretation unchanged

There are currently three common ways to change the byte order of a NUMpy array:

  • When the data does not match the dTYPE byte sequence, the DTYPE match data needs to be changed
  • When the data does not match the dTYPE byte sequence, the data needs to be changed to match the DTYPE
  • When the data matches the dTYPE order, the data is expected to be exchanged with the DTYPE

conclusion

This issue, numPY module for array internal storage, byte sequence two rules of big-endian mode and small-endian mode for learning.

The large-end rule is commonly seen in the internal system storage of computers. Generally, IBM and Sun systems store in big-end mode, while X86 and DEC CPU systems use small-end mode.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time