遇到了波浪线(位非 NOT)运算符的问题。

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

Experiencing an issue with the tilde (Bitwise NOT) operator

问题

我一直在尝试使用位运算符,以下是我的代码:

#include <stdio.h>

int main() {
    int x = 0b110000;
    int y = ~x;
    printf("x: %d", x);
    printf("\ny: %d", y);
    return 0;
}

这段代码的输出结果是:

x: 48
y: -49

我不明白的是为什么 y=-49,因为我使用了位取反运算符,它不应该反转位并使 y=0b001111y=15)吗?

我尝试使用其他数字来检查是否有异常情况,但并没有。我尝试了从0到29的每个数字,y = -(x+1) 的模式都保持不变。

有人能解释为什么会发生这种情况吗?
任何帮助都将不胜感激。

英文:

I've been experimenting with bitwise operators, here's my code:

#include &lt;stdio.h&gt;

int main() {
    int x = 0b110000;
    int y = ~x;
    printf(&quot;x: %d&quot;, x);
    printf(&quot;\ny: %d&quot;, y);
    return 0;
}

The output of this code block is:

x: 48
y: -49

The thing I don't understand is why y=-49, because I used the bitwise NOT operator, shouldn't it reverse the bits and make y=0b001111 (y=15)?

I tried to use other numbers to check if it was an exception, it was not. I tried every number from 0 to 29 and the pattern of y = -(x+1) remained.

Can anyone explain why is this happening?
Any help is appreciated.

答案1

得分: 2

int y = ~x & 0x3F;
英文:

make y=0b001111 - no, it should not make such y, because x is 0b00000000000000000000000000110000. uint8_t x will be 0b00110000 and also will not make make y=0b001111 but 0b11001111.

You might want to leave only 6 least bits in the result:

int y = ~x &amp; 0x3F;

答案2

得分: 2

>我不明白的是为什么 y=-49

y 的值显示为 -49,因为 printf 语句将 y 的值以有符号整数 %d 的形式打印出来。
或者,您可以选择将 y 的值以无符号整数的形式打印出来,使用 printf("y: %u\n", y)
这个结果将是-
x = 48
y = 4294967247

>因为我使用了位取反运算符,它不应该反转位并使 y=0b001111 (y=15) 吗?

假设在您的系统上,整数的大小为 32 位,在执行位取反操作时,存储在 y 中的值为:
二进制中的 11111111111111111111111111001111
(这与有符号的二进制补码中的 -49,十进制中的 4294967247 或十六进制中的 0XFFFFFFCF 相同。)

为了获得所需的结果,您可能需要使用位与运算符 & 来屏蔽掉不需要的位。

例如:

#include <stdio.h>

int main() {

    int x = 48;
    int y = ~x;
    int mask = 0xff;  // 所有 8 位都设为 1

    y = y & mask;
    printf("x: %d", x);
    printf("\ny: %d\n", y);

    return 0;
}
英文:

>The thing I don't understand is why y=-49

The value of y is displayed as -49 as the printf statement is printing the value of y as a signed integer - %d.
Alternatively, you could choose to print the value of y as an unsigned integer using printf(&quot;y: %u\n&quot;, y).
This result would be-
x = 48
y = 4294967247

>because I used the bitwise NOT operator, shouldn't it reverse the bits and make y=0b001111 (y=15)?

Assuming that the size of an integer on your system is 32 bits, when the bitwise NOT operation is performed, the value stored at y is:
11111111111111111111111111001111 in binary.
(Which is the same as -49 in Signed 2's complement or 4294967247 in decimal or 0XFFFFFFCF in hexadecimal.)

In order for you to get the desired result, you might have to use the bitwise &amp; operator to mask off the undesired set of bits.

For example:

#include &lt;stdio.h&gt;

int main() {

    int x = 48;
    int y = ~x;
    int mask = 0xff;  //All 8 bits set to 1

    y = y &amp; mask;
    printf(&quot;x: %d&quot;, x);
    printf(&quot;\ny: %d\n&quot;, y);

    return 0;
}

答案3

得分: 0

NOT运算符的过程是将所有的1转换为0,所有的0转换为1。如果标志位(sign flag)被设置,那么该数字将被视为负数(-ve),否则将被视为正数(+ve)。

在您的示例中,您的两个变量都是32位整数。因此,从右边的第6位到第31位的位将被转换为1。这就是为什么结果是-49的原因。

至于标志位,它之所以被设置,是因为在补码运算中进行了某种加法运算。

英文:

The procedure for the NOT operator is to convert all ones (1's) to zeros (0's) and all zeros (0's) to ones (1's). If the sign flag is set then the number will be regarded as negative (-ve), else it will be regarded as positive (+ve).

In your example, both of your variables are 32-bit integers. So bits 6 to 31 (starting at 0 from the right bit) will be converted to ones (1's). That is why the result is -49.

As for the sign flag, it was set because an addition was done somewhere during the complement.

huangapple
  • 本文由 发表于 2023年6月26日 02:31:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551885.html
匿名

发表评论

匿名网友

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

确定