关于小尾数和大尾数的按位非运算符(C中的〜) [英] Bitwise Not Operator (~ in C) with regards to little endian and big endian
问题描述
这与家庭作业有关,但与家庭作业无关。
This is in relation to a homework assignment but this is not the homework assignment.
我很难理解按位不(在C语言中是〜
)如何存在差异在大型字节序计算机与小型字节序计算机上编译时,受影响的 signed int
和 unsigned int
。
I'm having difficultly understanding if there is a difference on how the bitwise not (~
in C) would affected signed int
and unsigned int
when compiled on a big endian machine vs. a little endian machine.
确实是个字节是向后吗?如果不是,按位运算符(和其他运算符)是否会导致不同的结果会根据机器类型产生int
吗?
Are the bytes really "backwards" and if so does the bitwise not (and other operators) cause different resulting int
s be produced depending on the machine type?
虽然我们这样做了,但对于每个按位运算符,答案都是一样的吗? C还是严重依赖?
While we are at it, is the answer the same for each of the bitwise operators in C or does it heavily depend?
我要指的运算符是:
~ /* bitwise Not */
& /* bitwise And */
| /* bitwise Or */
^ /* bitwise Exclusive-Or */
谢谢
更新:到目前为止,在阅读我的回答时,我不得不问问按位非运算符是否会影响符号上的符号 signed int
。恐怕我在这方面有些困惑,因为我忘记了所有的静止状态。亚当似乎在说所有值都被视为未签名。是重新应用符号位还是曾经签名的值变为无符号?
Update: In reading my responses thus far, I feel compelled to ask if the bitwise not operator affects the sign bit on a signed int
. I'm afraid I've been a bit confused on this part as I forgot about all that stillyness. Adam seems to be stating that all values are treated as unsigned. Is the sign-bit reapplied or does the once signed value become unsigned?
推荐答案
按位运算符和逻辑运算符在大端和小端的机器。同样,它们对带符号和无符号数据也进行相同的运算:结果就好像所有内容都是无符号的一样。因此,如果 x
和 y
是有符号整数,则〜x
等于(int)(〜((unsigned int)x)
和 x& y
等于(int)((((unsigned int)x)&((unsigned int)y)
,对于所有其他运算符,依此类推。
The bitwise operators and logical operators all operate identically in big- and little-endian machines. Likewise, they also operator identically on signed and unsigned data: the result is as if everything were unsigned. So, if x
and y
are signed integers, then ~x
equals (int)(~((unsigned int)x)
and x & y
equals (int)(((unsigned int)x) & ((unsigned int)y)
, and so on for all of the other operators.
这篇关于关于小尾数和大尾数的按位非运算符(C中的〜)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!