[英]big endian vs little endian
i work on a 64 bit intel processor...i was learning about big and little endian and what i understood was that these are byte orderings within a word such that in a 64 bit data, msb will have lowest address in big endian form and the highest address in little endian form...now i have a problem:我在 64 位英特尔处理器上工作......我正在学习大端和小端,我的理解是这些是一个字内的字节顺序,因此在 64 位数据中,msb 将具有大端形式的最低地址并且little endian 形式的最高地址......现在我有一个问题:
I wrote this code我写了这段代码
to determine whether my processor was little or big endian...确定我的处理器是小端还是大端...
I input我输入
0102030405060708 (this is in hex)
and hoped to get 08
and 07
and 06
and... and 01
as answer并希望得到08
和07
和06
以及...和01
作为答案
but instead got 0
and 25
and 50
and -125
and -13
and 501
and -41
and 66
.而是得到0
and 25
and 50
and -125
and -13
and 501
and -41
and 66
。
when I wrote the same code taking 's' as 2 byte(short), the output for 0102
was 2
and 1
(which is in accordance with little endian)...so what went wrong here?当我编写相同的代码时,将“s”作为 2 个字节(短), 0102
的 output 是2
和1
(符合小端法)...所以这里出了什么问题?
You are storing your input value as a double
, which stores the value as a floating point value.您将输入值存储为double
,它将值存储为浮点值。 Try using a long long
instead, which is a 64 bit integer, and should store the value as you expect.尝试改用long long
,它是 64 位 integer,并且应该按预期存储值。
Taking a hex number into a (double)
is not likely to do what you expect;将十六进制数转换为(double)
不太可能达到您的预期; it's a floating point value consisting of a base 2 mantissa and exponent.它是一个浮点值,由以 2 为底的尾数和指数组成。 You might find (long)
or (long long)
to be closer to what you intended.您可能会发现(long)
或(long long)
更接近您的预期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.