tags:

views:

272

answers:

6

Hi

I'm dealing with some C code that includes

return ~0;

What does that mean? It's pretty much impossible to google for...

+10  A: 

~ is a bitwise not/complement, aka it changes all 0's to 1's and vice-versa. ~0 is a value with all bits set to 1.

A: 

Not zero or True.

ralu
It might not be good coding practice by the person who wrote "return ~0", but this answer provides a reasonable insight 50/50 probability of relevancy. It's good that you're trying to explain something and not just going for the limited, mechanical answer. I'd just qualify if by saying "look at how the calling code uses the return value... if it's always treating it as a boolean, then the programmer must be using it as a notation convertible to truth".
Tony
A bogus notation for truth; C has separate bitwise and logical operators for a reason. `!0` could make sense, but `~0` is nonsense (and as others have said, implementation-defined, so it should never be used except in hardware-specific bit-twiddling code).
R..
Of course, but that is only logical reason for doing this, why should someone return this for any other reason? True is in most cases defined as -1 or !0 when return value is int.
ralu
@ralu: That's right, but `~0` differs in that it signifies more than just "not zero" (so does `-1`). I think the statement "Not zero or true" is less correct than alternative definitions.
Matt Joiner
+2  A: 

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 1s to 0s

codaddict
`~` is the bitwise NOT, as @user446034 pointed out in his answer. The ones' complement is a representation scheme for numbers in binary format that uses this operator.
Space_C0wb0y
A: 

The tilde does a bitwise compliment of the number 0, which returns back a value with all bits set to 1, with whatever size of the return value (so you'd get 0xFF for a char, etc.)

Shaggy Frog
This is C. If you apply `~x` where `x` has type `char`, you will get the same thing as `~(int)x` due to default promotions, **not** `0xff`.
R..
@R.. This occurred to me also, and now I'm stuck with an upvote
Matt Joiner
A: 

There are two independent parts here: return and ~0.

return is a return statement. Read about it in your favorite C book.

~0 is an expression consisting of bitwise-complement operator ~ applied to integer constant 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. On a two's complement machine a signed integral value with such bit pattern (111...1) would represent -1.

AndreyT
+3  A: 

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.

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. That latter hypothesis is easily checked, and googling for "C language operator" will quickly lead to dozens of tables of operators. 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.

On the majority of platforms you will encounter, that integer has the value -1.

RBerteig