简体   繁体   English

在C语言中,“return~0”是什么意思?

[英]In the C language, what does “return ~0” mean?

I'm dealing with some C code that includes 我正在处理包含的一些C代码

return ~0;

What does that mean? 这意味着什么? It's pretty much impossible to google for... google几乎不可能......

~ is a bitwise not/complement, aka it changes all 0's to 1's and vice-versa. 〜是一个按位not / complement,也就是说它将所有0改为1,反之亦然。 ~0 is a value with all bits set to 1. ~0是一个值,所有位都设置为1。

The key to answering this class of question as you inspect the code is to recognize enough of the structure of the language to know what question to ask. 在检查代码时回答这类问题的关键是要认识到语言的足够结构,以便知道要问什么问题。 For example, the return statement requires an expression, of a type compatible with the declared return type for the function itself. 例如, return语句需要一个表达式,该表达式与函数本身的声明返回类型兼容。

Knowing that ~0 must be an expression, it is either a really funny way to write a number, or it is an operator you don't recognize applied to the constant zero. 明知~0必须是一个表达式,它或者是写一个数一个非常有趣的方式,或者是你不承认应用于恒零操作。 That latter hypothesis is easily checked, and googling for "C language operator" will quickly lead to dozens of tables of operators . 后一个假设很容易检查,谷歌搜索“C语言操作员”将迅速导致数十个运营商表 Nearly any one of which will tell you that the ~ operator is a bitwise-not unary operator which inverts each individual bit of its operand. 几乎任何一个都会告诉你, ~运算符是一个按位而非一元运算符,它反转其操作数的每个位。 In this specific case, that converts the signed integer 0 to the integer represented with all its bits set. 在这种特定情况下,将有符号整数0转换为用其所有位设置表示的整数。

On the majority of platforms you will encounter, that integer has the value -1. 在您将遇到的大多数平台上,该整数的值为-1。

The ~ (tilde) operator performs a bitwise complement on its single integer operand. ~ (代字号)运算符对其单个整数操作数执行按位补码。

Complementing a number means to change all the 0 bits to 1 and all the 1 s to 0 s 补充数字意味着将所有0位更改为1并将所有1 s更改为0 s

Anyway, for search queries with special symbols like yours "return ~0;" 无论如何,对于像你这样的特殊符号的搜索查询“return~0;” you can use http://symbolhound.com/ 你可以使用http://symbolhound.com/

It is pretty useful for programmer. 它对程序员非常有用。

There are two independent parts here: return and ~0 . 这里有两个独立的部分: return~0

return is a return statement. return是一个return语句。 Read about it in your favorite C book. 在你最喜欢的C书中阅读它。

~0 is an expression consisting of bitwise-complement operator ~ applied to integer constant 0 . ~0是由按位补码运算符~应用于整数常数0组成的表达式。 All bits in a zero value of type int are inverted (become 1) and the resultant int value (with all bits set to 1) is what the ~0 expression evaluates to. 在类型的零值的所有位int被反转(成为1),将所得int值(具有设置为1的所有比特)是什么~0表达式求。 On a two's complement machine a signed integral value with such bit pattern ( 111...1 ) would represent -1 . 在二进制补码机器上,带有这种位模​​式( 111...1 )的带符号整数值表示-1

不是零或真。

代字号对数字0进行逐位补充,返回一个值,所有位都设置为1,返回值的大小(所以你得到一个字符的0xFF等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM