Sobes.tech
Junior — Middle

Explain the principle of byte order little-endian in computer memory.

sobes.tech AI

Answer from AI

In little-endian order, the least significant byte of a number is stored at the lower memory address, and the most significant byte at the higher. For example, a 32-bit number 0x12345678 in memory will be arranged as a sequence of bytes: 0x78, 0x56, 0x34, 0x12.

This means that when reading bytes from the lower address, we first get the least significant byte of the number. This order is convenient for some operations and is used in x86 architectures.

An example in Go to demonstrate writing a number in little-endian:

package main

import (
	"encoding/binary"
	"fmt"
)

func main() {
	var num uint32 = 0x12345678
	buf := make([]byte, 4)
	binary.LittleEndian.PutUint32(buf, num)
	fmt.Printf("% x\n", buf) // Will output: 78 56 34 12
}