%x 和 ~ 的意义

发布于 2024-12-27 15:36:27 字数 165 浏览 1 评论 0原文

int m=32
printf("%x" , ~m);

此语句的输出为 ffdf,没有 ~ 的输出为 20%x~ 有何意义?

int m=32
printf("%x" , ~m);

Output of this statement is ffdf and without ~ output is 20.
What is the significance of %x and ~?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心作怪 2025-01-03 15:36:27

~ 运算符是按位求反。它将打印 m 值的按位否定。 %x 表示 printf 将以十六进制格式输出其值。

因此,值 0xffdf 是值 0x20 (32) 的否定。

值 32(int 位将是):

0000 0000 0010 0000

它的按位求反将是:

1111 1111 1101 1111

这是有道理的,因为:

1111 1111 = 0xff

并且:

1101 1111 = 0xdf

The ~ operator is bitwise negation. It will print bitwise negation of m's value. %x means that printf will output its value in hexadecimal format.

So, value 0xffdf is the negation of value 0x20 (32).

Value 32 (int bits would be):

0000 0000 0010 0000

Its bitwise negation will be:

1111 1111 1101 1111

Which makes sense since:

1111 1111 = 0xff

And:

1101 1111 = 0xdf
娇纵 2025-01-03 15:36:27

%xprintf 格式 表示 int 值应以十六进制显示。

~按位 NOT,它翻转所有位整数。

该语句:

printf("%x", m);

将输出 20 显示为 0x20 = 十进制 32

该语句:

printf("%x", ~m);

将显示输出 ffdf,因为 0xffdf0x20 的按位反转。

以二进制形式可视化按位求反可能更有意义:

Base 10:         32                  65503
Base 16:        0x20                0xFFDF
Base 2:    0000000000100000    1111111111011111

The %x is the printf format that indicates that the int value should be displayed in hexadecimal.

The ~ is bitwise NOT, which flips all the bits in the integer.

The statement:

printf("%x", m);

will display the output 20 as 0x20 = decimal 32.

The statement:

printf("%x", ~m);

will display the output ffdf as 0xffdf is the bitwise inverse of 0x20.

It may make more sense to visualize the bitwise negation in binary:

Base 10:         32                  65503
Base 16:        0x20                0xFFDF
Base 2:    0000000000100000    1111111111011111
够钟 2025-01-03 15:36:27

这意味着 x 可能应该被声明为 unsigned 而不是 int

"%x" printf 格式需要一个 unsigned int 参数,并以十六进制(基数 16)打印其值。您可以安全地将其与 int 参数一起使用如果该值位于可表示为 intunsigned int 的范围内(即 0 ..INT_MAX)。正如此代码片段所示,使用带有负 int 值的 "%x" 严格来说具有未定义的行为,尽管实际行为可能相当一致。

C 标准说:

~运算符的结果是其按位求补
(提升)操作数(即,结果中的每一位都被设置当且仅
如果转换后的操作数中的相应位未设置)。

请注意,它是根据操作数的表示形式而不是其值来定义的。

您描述的输出表明您正在使用具有二进制补码有符号整数的系统,其中 int 只有 16 位,这在当今是不常见的。 (您是否使用古老的 Turbo C 编译器或类似的编译器?)在我的系统上,此程序:

#include <stdio.h>
int main(void) {
    int m = 32;
    printf("%x\n" , ~m);
    return 0;
}

产生以下输出:(

ffffffdf

请注意,我已添加所需的 #includem 声明上的分号。)

It means that x should have probably have been declared unsigned rather than int.

The "%x" printf format requires an unsigned int argument, and prints its value in hexadecimal (base 16). You can safely use it with an int argument if the value is within the range representable either as an int or as an unsigned int (i.e., 0 .. INT_MAX). Using "%x" with a negative int value, as this code fragment does, strictly speaking has undefined behavior, though the actual behavior is likely to be reasonably consistent.

The C standard says:

The result of the ~ operator is the bitwise complement of its
(promoted) operand (that is, each bit in the result is set if and only
if the corresponding bit in the converted operand is not set).

Note that it's defined in terms of the representation of the operand, not its value.

The output you describe indicates that you're using a system with two's-complement signed integers where int is only 16 bits, which is unusual these days. (Are you using an ancient Turbo C compiler or something similar?) On my system, this program:

#include <stdio.h>
int main(void) {
    int m = 32;
    printf("%x\n" , ~m);
    return 0;
}

produces this output:

ffffffdf

(Note that I've added the required #include <stdio.h> and a semicolon on the declaration of m.)

苯莒 2025-01-03 15:36:27

~ 符号代表按位非,或补码运算符对每个位执行逻辑非的一元运算,形成给定二进制值的补码。二进制数字中 0 变为 1,1 变为 0。32

是二进制的 00100000,~32 是二进制的 11011111(或十进制的 223)。

printf 函数中的%x 选项将显示无符号的十六进制格式(使用小写字母)。

所以,

printf("%x", m); // displays the hexadecimal value of 32 (00100000), "20"

printf("%x", ~m); // displays the hexadecimal value of ~32 (11101111), "ffdf"

[来源]
http://en.wikipedia.org/wiki/Bitwise_operation#NOT
http://en.wikipedia.org/wiki/Hexadecimal
http://en.wikipedia.org/wiki/Printf_format_string

The ~ symbol represents the bitwise NOT, or complement operator; a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Binary digits that are 0 become 1, and those that are 1 become 0.

32 is 00100000 in binary, and ~32 is 11011111 in binary (or 223 in decimal).

The %x option in the printf function will display a unsigned hexadecimal format (using lowercase letters).

So,

printf("%x", m); // displays the hexadecimal value of 32 (00100000), "20"

printf("%x", ~m); // displays the hexadecimal value of ~32 (11101111), "ffdf"

[Sources]
http://en.wikipedia.org/wiki/Bitwise_operation#NOT
http://en.wikipedia.org/wiki/Hexadecimal
http://en.wikipedia.org/wiki/Printf_format_string

夜声 2025-01-03 15:36:27

%x 表示以十六进制打印 x 的值。

And %x means you print the value of x in hexadecimale.

遗忘曾经 2025-01-03 15:36:27

~32 = -33 使用 unsigned int 获取结果

~32 = -33 use unsigned int to get results

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文