Endianness¶
Objective¶
Practice working with both big and littleendian bit and byte ordering.
BigEndian and LittleEndian Notation¶
In decimal (base10) systems, each digit of a number can have 10 possible values: 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9. The placement of the digit corresponds to which power of 10 it represents. The number 891, for example, is really just an easy way to represent this addition of powers of 10:
This particular notation, where the largest power of 10 is the leftmost digit, is called bigendian notation. This is the way that we think of numbers in everyday life. The opposite convention, where the leftmost digit is the smallest power of 10, is called littleendian notation. In littleendian notation, the number eighthundred ninetyone would be written as 198. As a more general formula for littleendian numbers:
where the coefficient \(x_i\) means the \(i\)th digit from the left (so \(x_0\) is the first digit, \(x_1\) is the second digit, and so on). This may seem bizarre, but it's actually the way many computers keep track of values in memory. Which one you use depends on what you're doing. Knowing the difference between them, and recognizing numbers in either format, is going to be helpful when working with quantum algorithms. Many algorithms are defined in academic papers, and some authors prefer one format over the other, so you'll end up working with both fairly often.
Bitwise vs Bytewise Endianness¶
The above description of endianness can be applied directly to binary numbers and data. In bitwise bigendian notation, the leftmost bit is the most significant bit. In bitwise littleendian notation, the leftmost bit is the least significant bit.
However, conventional computers group data by bytes (8 bits). So if computer is said to have a littleendian architecture, that is usually referring to the byte ordering rather than the bit ordering. For bytewise littleendian data, the leftmost byte is the least significant byte. For example, the data 0x12345678
would be stored as 0x78563412
in littleendian byte ordering.
Additional Materials¶

Wikipedia article on endianness
 Focus on the "Byte addressing" and "Bit endianness" sections.
Knowledge Check¶
Q1¶
What is the littleendian bit ordering of the bigendian data, 10010111
?
Q2¶
What is the decimal value of the bitwise littleendian data, 1010
?
Q3¶
What is the littleendian byte ordering of the bigendian data, 0xBEEF
?
Q4¶
What is the decimal value of the bytewise littleendian data, 0x2800
?
Q5¶
(Hard) Convert the bigendian data 0x1234 to littleendian byte ordering, with the bits in each byte ordered in little endian. Give your answer in hexadecimal.