Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

When Do We Have to Swap Bytes?G.5

When moving a soundfile from a ``PC'' or modern Mac to an old ``PowerPC Mac'' (Intel or ARM processor to Motorola processor), the bytes in each sound sample need to be swapped. This is because Motorola processors are big endian (bytes are numbered from most-significant to least-significant in a multi-byte word) while Intel processors are little endian (bytes are numbered from least-significant to most-significant).G.6 Any Mac program that supports a soundfile format native to PCs (such as .wav files) will swap the bytes for you. You only have to worry about swapping the bytes yourself when reading raw binary soundfiles from a foreign computer, or when digging the sound samples out of an ``unsupported'' soundfile format yourself.

Since soundfiles typically contain 16 bit samples (not for any good reason, as we now know), there are only two bytes in each audio sample. Let L denote the least-significant byte, and M the most-significant byte. Then a 16-bit word is most naturally written $ [M,L] = M\cdot 256 + L$ , i.e., the most-significant byte is most naturally written to the left of the least-significant byte, analogous to the way we write binary or decimal integers. This ``most natural'' ordering is used as the byte-address ordering in big-endian processors:

   M,L, M,L, M,L, ..., M,L   (Big Endian)

Little-endian machines, on the other hand, store bytes in the order

   L,M, L,M, L,M, ..., L,M$\displaystyle . \protect$   (Little Endian)

These orderings are preserved when the sound data are written to a disk file.

Since a byte (eight bits) is the smallest addressable unit in modern day processor families, we don't have to additionally worry about reversing the bits in each byte. Bits are not given explicit ``addresses'' in memory. They are extracted by means other than simple addressing (such as masking and shifting operations, table look-up, or using specialized processor instructions).

Table G.6 lists current and earlier processors and their ``endianness'':G.7

Table G.6: Byte ordering in various computing platforms.
Processor Family Endian
x86 (Intel Core i&sstarf#star;, Pentium, Celeron) Little
ARM (M1,M2,M3) Little
Alpha (DEC/Compaq) Little
680x0 (Motorola) Big
PowerPC (Motorola & IBM) Big
SPARC (Sun) Big

The following Python script prints the endianness of the processor you are running on:

from sys import byteorder

When compiling C or C++ programs under UNIX/Linux, there may be a macro constant called BYTE_ORDER in the header file endian.h or bytesex.h. In other cases, there may be macros such as __INTEL__, __LITTLE_ENDIAN__, __BIG_ENDIAN__, or the like, which can be detected at compile time using #ifdef.

Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

[How to cite this work]  [Order a printed hardcopy]  [Comment on this page via email]

``Mathematics of the Discrete Fourier Transform (DFT), with Audio Applications --- Second Edition'', by Julius O. Smith III, W3K Publishing, 2007, ISBN 978-0-9745607-4-8
Copyright © 2024-04-02 by Julius O. Smith III
Center for Computer Research in Music and Acoustics (CCRMA),   Stanford University