go binary large end of the small end of understanding

package main

import (
	"fmt"
	"unsafe"
)
const INT_SIZE int = int(unsafe.Sizeof(0))

func main() {
	systemEdian()
}

func systemEdian() {
	var i int = 0x1234
	bs := (*[INT_SIZE]byte)(unsafe.Pointer(&i))
	fmt.Println(*bs)
	fmt.Println(&bs[0])
	fmt.Println(&bs[1])
	fmt.Println(&bs[2])
	fmt.Println("...")
	if bs[0] == 52 {
		fmt.Println("system edian is little endian")
	} else {
		fmt.Println("system edian is big endian")
	}
}

Output:

[52 18 0 0 0 0 0 0]
0xc000084000
0xc000084001
0xc000084002
...
system edian is little endian

 

52=0x34 18=0x12

Low 0x34 stored in the lower address 0xc000084000

0x12 high in high address 0xc000084001

So for the small end

If it shows [1852000000], the human readable 0x1234, but not conducive to computer calculation

 

to sum up:

The big end: high stored at the lower address, stored in the high-low

Small end: high in high address, low address is stored in the low

 

Question: Why are there little-endian?

Because the computer processing circuit low byte first, high efficiency, are calculated from the lower bits, for example 45 * 4, 4 * 5 first count low, then count the high 4 * 4

Therefore, the internal processing is little endian computers.

However, humans still used to read and write big endian. Therefore, in addition to the internal processing of the computer, where almost all of the other big endian, such as network file transfer and storage.

Guess you like

Origin www.cnblogs.com/lpfuture/p/12456164.html