位移和字节顺序 [英] Bit shifts and endianness

查看:150
本文介绍了位移和字节顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我今天在想,假设我们有这个数字

n = 0xAB 0xFF

这相当于至十进制44031。在big endian中它将是

存储为

10101011 11111111

但是在小端,它将是

11111111 10101011

如果我们然后应用位移n<< 2;这将给我们完全不同的数字
。所以实际上是左边的一个左移位

a big-endian和小端的左移位实际上是一个正确的内存中的
移位?


非常感谢


PS。字节序还会产生哪些其他问题?有没有什么好的

资源?

Hi all,

I was thinking today, suppose we have the number
n = 0xAB 0xFF
which is equivalent to 44031 in decimal. In big endian it will be
stored as
10101011 11111111
but in little endian it will be
11111111 10101011
If we then apply a bit shift n << 2; that would give us completely
different numbers. So is actually a left bit shift done to the left on
a big-endian and left bit shift on little endian is actually a right
shift in the memory?

Thanks a lot

PS. What other problems arise from endianness? Are there any good
resources on that?

推荐答案

" gamehack" < GA ****** @ gmail.com>写道:
"gamehack" <ga******@gmail.com> writes:
我今天在想,假设我们有数字
n = 0xAB 0xFF


这是两个数字。你的意思是0xABFF吗?

相当于44031的十进制数。


显然是这样。

在big endian中它将被存储为
10101011 11111111
但是在小端将是
11111111 10101011
如果我们然后应用位移n<< 2;这将给我们完全不同的数字。
I was thinking today, suppose we have the number
n = 0xAB 0xFF
That''s two numbers. Do you mean 0xABFF?
which is equivalent to 44031 in decimal.
Apparently so.
In big endian it will be
stored as
10101011 11111111
but in little endian it will be
11111111 10101011
If we then apply a bit shift n << 2; that would give us completely
different numbers.




编号。移位运算符在其操作数的*值*上定义,

不在他们的陈述上。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



No. Shift operators are defined on the *values* of their operands,
not on their representations.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.




Keith Thompson写道:

Keith Thompson wrote:
" gamehack" < GA ****** @ gmail.com>写道:
"gamehack" <ga******@gmail.com> writes:
我今天在想,假设我们有数字
n = 0xAB 0xFF
这是两个数字。你的意思是0xABFF吗?
I was thinking today, suppose we have the number
n = 0xAB 0xFF
That''s two numbers. Do you mean 0xABFF?



对不起,我的意思是0xABFF并将它们分开为不同的字节。


Sorry, I meant 0xABFF and just separated them as distinct bytes.

相当于44031的十进制数。
显然是这样。
which is equivalent to 44031 in decimal.
Apparently so.
在big endian中它将被存储为
10101011 11111111
但是在小端将会是
11111111 10101011
如果我们然后应用位移n<< 2;这将给我们完全不同的数字。
In big endian it will be
stored as
10101011 11111111
but in little endian it will be
11111111 10101011
If we then apply a bit shift n << 2; that would give us completely
different numbers.



No.转移运算符是在其操作数的*值*上定义的,
不在它们的表示中。



No. Shift operators are defined on the *values* of their operands,
not on their representations.



谢谢 -
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。


Thanks --
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.






2006年1月5日星期四20:26:11 GMT,在comp.lang.c中, Keith Thompson

< ks *** @ mib.org>写道:
On Thu, 05 Jan 2006 20:26:11 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
" gamehack" < GA ****** @ gmail.com>写道:
"gamehack" <ga******@gmail.com> writes:
我今天想的是,假设我们有数字
10101011 11111111
但是在小端将会是
11111111 10101011
如果我们那么应用位移n<< 2;这会给我们完全不同的数字。
I was thinking today, suppose we have the number 10101011 11111111
but in little endian it will be
11111111 10101011
If we then apply a bit shift n << 2; that would give us completely
different numbers.



没有。 Shift操作符在其操作数的*值*上定义,
不在其表示中。



No. Shift operators are defined on the *values* of their operands,
not on their representations.




澄清:我认为你的意思是移位操作符在

二进制值,无论它是如何存储在磁盘上或内存中的。

我这么说是因为很少有人会想到0xabff的值
作为二进制值。 :-)


(请不要再开始另一个单调乏味的了,我说的很好'

''对''' ,所以你真是个傻瓜再次讨论,我真的很好。

我不想表达我对那些不能接受的人的意见

那个可能不止一种方法给猫皮肤了。)

马克麦金太尔

-


---- = =通过Newsfeeds.Com发布 - 无限制 - 无限制安全Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----



Clarification: I assume you mean that shift operators operate on a
binary value, irrespective of how that is stored on disk or in memory.
I''m saying this because few humans would think of the value of 0xabff
as the value in binary. :-)

(and please, don''t start another one of those tedious "well patently
what I said is ''right'', so you''re a fool" discussions again, I really
don''t want to have to express my opinion of people who can''t accept
that there may be more than one way to skin a cat).
Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


这篇关于位移和字节顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆