%x 和 ~ 的意义
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
~
运算符是按位求反。它将打印m
值的按位否定。%x
表示printf
将以十六进制格式输出其值。因此,值
0xffdf
是值0x20
(32) 的否定。值 32(int 位将是):
它的按位求反将是:
这是有道理的,因为:
并且:
The
~
operator is bitwise negation. It will print bitwise negation ofm
's value.%x
means thatprintf
will output its value in hexadecimal format.So, value
0xffdf
is the negation of value0x20
(32).Value 32 (int bits would be):
Its bitwise negation will be:
Which makes sense since:
And:
%x
是printf
格式 表示int
值应以十六进制显示。~
是 按位 NOT,它翻转所有位整数。该语句:
将输出
20
显示为0x20
= 十进制32
。该语句:
将显示输出
ffdf
,因为0xffdf
是0x20
的按位反转。以二进制形式可视化按位求反可能更有意义:
The
%x
is theprintf
format that indicates that theint
value should be displayed in hexadecimal.The
~
is bitwise NOT, which flips all the bits in the integer.The statement:
will display the output
20
as0x20
= decimal32
.The statement:
will display the output
ffdf
as0xffdf
is the bitwise inverse of0x20
.It may make more sense to visualize the bitwise negation in binary:
这意味着
x
可能应该被声明为unsigned
而不是int
。"%x"
printf 格式需要一个unsigned int
参数,并以十六进制(基数 16)打印其值。您可以安全地将其与int
参数一起使用如果该值位于可表示为int
或unsigned int 的范围内
(即 0 ..INT_MAX
)。正如此代码片段所示,使用带有负int
值的"%x"
严格来说具有未定义的行为,尽管实际行为可能相当一致。C 标准说:
请注意,它是根据操作数的表示形式而不是其值来定义的。
您描述的输出表明您正在使用具有二进制补码有符号整数的系统,其中
int
只有 16 位,这在当今是不常见的。 (您是否使用古老的 Turbo C 编译器或类似的编译器?)在我的系统上,此程序:产生以下输出:(
请注意,我已添加所需的
#include
和m
声明上的分号。)It means that
x
should have probably have been declaredunsigned
rather thanint
.The
"%x"
printf format requires anunsigned int
argument, and prints its value in hexadecimal (base 16). You can safely use it with anint
argument if the value is within the range representable either as anint
or as anunsigned int
(i.e., 0 ..INT_MAX
). Using"%x"
with a negativeint
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:
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:produces this output:
(Note that I've added the required
#include <stdio.h>
and a semicolon on the declaration ofm
.)~
符号代表按位非,或补码运算符; 对每个位执行逻辑非的一元运算,形成给定二进制值的补码。二进制数字中 0 变为 1,1 变为 0。32是二进制的 00100000,~32 是二进制的 11011111(或十进制的 223)。
printf 函数中的%x 选项将显示无符号的十六进制格式(使用小写字母)。
所以,
[来源]
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 theprintf
function will display a unsigned hexadecimal format (using lowercase letters).So,
[Sources]
http://en.wikipedia.org/wiki/Bitwise_operation#NOT
http://en.wikipedia.org/wiki/Hexadecimal
http://en.wikipedia.org/wiki/Printf_format_string
%x 表示以十六进制打印 x 的值。
And %x means you print the value of x in hexadecimale.
~32 = -33 使用 unsigned int 获取结果
~32 = -33 use unsigned int to get results