“-1 & postive integer” 总是返回正整数吗?

huangapple go评论116阅读模式
英文:

Why "-1 & postive integer" alway return the postive integer?

问题

例如:

printf("%d", -1 & 3);

输出结果为3。我尝试了其他数字,结果都一样。
抱歉问题很简短,但我认为没有必要无谓地延长它。

我搜索了一下,没有找到答案。

英文:

For example:

printf("%d", -1 & 3); 

The output is 3. I tried with other number and it's the same.
sorry for short question, but I don't think I should make it long for no reason.

I searched around and did not find answer to it.

答案1

得分: 3

Some1) (not all) of the bitwise operators don't care about signedness of the operands but instead look at the raw binary representation of the variable.

In case of the negative value -1, the raw binary in the common two's complement format looks as 1111....1111 (all ones).

As an example, lets assume 16 bit int, 2's complement:

Dec   Bin
-1    1111 1111 1111 1111
 3    0000 0000 0000 0011
-------------------------
 &     0000 0000 0000 0011 = 3 dec

1) Specifically, & | ^ ~ and their compound assignment equivalents only look at the raw binary representation.

英文:

Some<sup>1)</sup> (not all) of the bitwise operators don't care about signedness of the operands but instead look at the raw binary representation of the variable.

In case of the negative value -1, the raw binary in the common two's complement format looks as 1111....1111 (all ones).

As an example, lets assume 16 bit int, 2's complement:

Dec   Bin
-1    1111 1111 1111 1111
 3    0000 0000 0000 0011
-------------------------
 &amp;    0000 0000 0000 0011 = 3 dec

<sup>1)</sup> Specifically, &amp; | ^ ~ and their compound assignment equivalents only look at the raw binary representation.

答案2

得分: 0

因为这是按位运算。两个补码的负数最高位设置为1,正数将这一位设置为零。

如果你执行 1 & 0 操作,结果将始终为零。因此,在这个操作之后,最高位将变为零,整数将变为正数

示例(x - 不重要)

1xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - 负数
0xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - 正数

1xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx(负数)& 0xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx(正数)= 0xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx(正数)

英文:

Because it is bitwise operation. Two complement's negative numbers have the most significant bit set, positive have this bit set to zero.

If you 1 &amp; 0 the result will be always zero. So after this operation the MSB will be zero and the integer will be positive

Example (x - does not matter)

1xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - negative number
0xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx - positive number

1xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx (negative) & 0xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx (positive) = 0xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx (positive)

答案3

得分: 0

为什么"-1 & 正整数"总是返回正整数?

C语言支持各种int编码。所有都有一个"符号"位。"必须恰好有一个符号位"。只有设置了该位,值才能为负数。

使用"&"永远不会设置任何位,包括"符号"位。"&"只会减少或保持已设置的位数。

"任何内容 & 正整数"总是会得到一个正数(非负数)。

英文:

> Why "-1 & positive integer" always return the positive integer?

C allows various int encodings. All have a sign bit. "There shall be exactly one sign bit." Only with that bit set may the value be negative.

Using &amp; will never set any bit including the sign bit. &amp; will only reduce - or keep the same - the bits that are set.

"anything & positive integer" will always result in a positive (non-negative) number.

huangapple
  • 本文由 发表于 2023年3月9日 19:30:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定