比特移位与架构问题。 [英] Bit shifting versus architecture question.

查看:69
本文介绍了比特移位与架构问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我理解不同的处理器硬件可能会以不同的顺序将这些位存储在一个
字节中。它是否会在C中产生差异

作为位移无符号字符变量?


例如,如果我有

unsigned char x = 1;

总是如此

(x << 1)== 2

(x< ;< 2)== 4

等?


或者我可能要改变某些转变的方向

架构?


(我正在抓住吸管试图找出为什么在我的i86系统上编写的某些C软件

在另一个系统上编译失败的原因

个人的PPC系统。)


感谢您的帮助。


问候,
Charles Sullivan


I understand different processor hardware may store the bits in a
byte in different order. Does it make a difference in C insofar
as bit-shifting unsigned char variables is concerned?

E.g, if I have
unsigned char x = 1;
is it always true that
(x << 1) == 2
(x << 2) == 4
etc?

Or might I have to reverse the direction of the shift on some
architectures?

(I''m grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual''s PPC system.)

Thanks for your help.

Regards,
Charles Sullivan

推荐答案

Charles Sullivan< cw ****** @ triad.rr.comwrites:
Charles Sullivan <cw******@triad.rr.comwrites:

例如,如果我有

unsigned char x = 1;

总是如此

(x << 1)== 2

(x << 2)== 4

等?
E.g, if I have
unsigned char x = 1;
is it always true that
(x << 1) == 2
(x << 2) == 4
etc?



是的,这总是正确的。 C中的移位运算符定义在数值的
项中,而不是如何表示这些值。

-

这是一个很棒的答案。

这是偏离主题的,它是不正确的,它没有回答这个问题。

- 理查德希思菲尔德

Yes, that''s always true. The shift operators in C are defined in
terms of numeric values, not how those values are represented.
--
"This is a wonderful answer.
It''s off-topic, it''s incorrect, and it doesn''t answer the question."
--Richard Heathfield


Charles Sullivan在02/07/07 14:46写道:
Charles Sullivan wrote On 02/07/07 14:46,:

我理解不同的处理器硬件可以按不同的顺序将这些位存储在一个
字节中。它是否会在C中产生差异

作为位移无符号字符变量?


例如,如果我有

unsigned char x = 1;

总是如此

(x << 1)== 2

(x< ;< 2)== 4

等?
I understand different processor hardware may store the bits in a
byte in different order. Does it make a difference in C insofar
as bit-shifting unsigned char variables is concerned?

E.g, if I have
unsigned char x = 1;
is it always true that
(x << 1) == 2
(x << 2) == 4
etc?



是。

Yes.


或者我可能需要在某些方面改变班次的方向

架构?
Or might I have to reverse the direction of the shift on some
architectures?



No.订单只有当你将两个较小的块组装成一个较大的块时才会发挥作用,或者将较大的块分解成较小的块。像大多数C运算符一样,移位运算符和b
代表的价值一样,无论它如何表示。因此,

即使这样:


int x = 128;

断言(x << 1 == 256) ;

断言(x <<<< 2 == 512);


在所有系统上运行相同,包括Big-和LittleEndian,

,即使单独的一位(可能)从一个字节移动到另一个字节


No. "Order" only comes into play when you assemble two
or more smaller pieces into one larger piece, or decompose
the larger piece into smaller fragments. The shift operators,
like most C operators, works with the value that all the
pieces represent, no matter how it is represented. Thus,
even this:

int x = 128;
assert (x << 1 == 256);
assert (x << 2 == 512);

works identically on all systems, both Big- and LittleEndian,
even though the lone one-bit moves (probably) from one byte
of x to another.


(我'我正在抓住吸管试图找出为什么在我的i86系统上写的一些C软件

在另一个

个人的PPC系统上编译时失败了。)
(I''m grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual''s PPC system.)



我不知道你所展示的释义是怎样的,b
负责。如果它被一些稍微改变的东西释义,你可以发布一个更大的东西的实际剪辑,

也许有人会发现有用的东西。


-
Er ********* @ sun。 com


Charles Sullivan写道:
Charles Sullivan wrote:

我理解不同的处理器硬件可能存储这些位a

字节的顺序不同。它是否会因为位移无符号字符变量而在C中产生差异?

I understand different processor hardware may store the bits in a
byte in different order. Does it make a difference in C insofar
as bit-shifting unsigned char variables is concerned?



编号硬件甚至不需要任何位。没有什么可以排除在三元计算机上实现C的实现。

No. The hardware is not even required to have any "bits". There''s nothing that
precludes a C implementation on, say, a ternary computer.


例如,如果我有

unsigned char x = 1;

总是如此

(x << 1)== 2

(x << 2)== 4

等?
E.g, if I have
unsigned char x = 1;
is it always true that
(x << 1) == 2
(x << 2) == 4
etc?



是的。 比特的概念在C中指的是传统二元的元素

表示法,而不是硬件位。 1在二进制文件中始终为1,而2在二进制文件中为bb始终为10。这意味着单个左移将始终将1

变为2.

Yes. The notion of "bit" in C refers to an element of the traditional binary
notation, not to the hardware bit. ''1'' is always ''1'' in binary , while ''2'' is
always ''10'' in binary. This means that a single left shift will always turn 1
into 2.


或者我可能必须改变转变的方向一些

架构?
Or might I have to reverse the direction of the shift on some
architectures?



No.


-

祝你好运,

Andrey Tarasevich

No.

--
Best regards,
Andrey Tarasevich


这篇关于比特移位与架构问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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