※ 引述《zzss2003 (brotherD)》之銘言:
: 網址: https://stackoverflow.com/questions/13926760/the-reason-behind-endianness
: 網址裡面有提到big跟little的優缺點,但小弟資質不良,無法完全理解,想請各位前輩開
: 釋
: little-endian:
: 1. The little-endian system has the property that the same value can be read
: from memory at different lengths without using different addresses
value 4A in little-endian
char [4A]
short [4A 00]
int [4A 00 00 00]
long [4A 00 00 00 00 00 00 00]
可以節省記憶體
https://en.wikipedia.org/wiki/Endianness#Optimization
: 請問,at different lengths without using different addresses是什麼意思?
: 2. When adding or subtracting multi-byte numbers, the least significant byte
: must be fetched first to see if there is a carryover to more significant
: bytes. Because the least-significant byte is read first in little-endian
: numbers, the system can parallelize and begin calculation on this byte while
: fetching the following byte(s).
msb lsb
[010010101 10111010]
+[010001100 11100010]
---------------------
不用考慮進位借位問題讀完第一個 lsb byte 就可以算
: 這一段我看得懂,大致上的意思是little-endian有辦法一邊fetch lower byte 一邊做計
: 算,如果是big-endian就必須全部fetch下來才可以做計算
: big-endian:
: 1. it is simple to tell whether a number is positive or negative by simply
: examining the bit at offset 0 in the lowest order byte.
跟比大小無關
-1022
little-endian [00000010 11111100]
big-endian [11111100 00000010]
big endian 讀第1個bit就可以知道是正是負
: 這邊我也看得懂,big-endian的優點就是很容易比出兩個數字的大小。
: 2. binary digits are ordered as most people order base-10 digits. This is
: advantageous performance-wise when converting from binary to decimal.
: 不懂,為什麼從2進制轉換成10進制會有performance-wise?
: 我認為不管是big還是little,轉成10進制的效能都是一樣的,只是big從MSB開始加,
: little從LSB開始加
: 請各位指點迷津,謝謝
假設
int a = e;
&a = 0xa
0xa [000000000]
0xb [100101001]
0xc [100010011]
0xd [111101011]
不論little big 要讀 a 的值都會至 address 0xa 讀值
big直出 a->b->c->d 讀下去
little 就要先 offset後 再d->c->b->a
直覺上效能會比 big 慢,但這個差異可忽略
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.143.169
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1524119221.A.913.html