Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ~ mean in C++?

Tags:

c++

Specifically, could you tell me what this line of code does:

int var1 = (var2 + 7) & ~7;

Thanks

like image 307
Swiss Avatar asked Jul 06 '10 14:07

Swiss


People also ask

What does '?' Mean in C programming?

Most likely the '?' is the ternary operator. Its grammar is: RESULT = (COND) ? ( STATEMEN IF TRUE) : (STATEMENT IF FALSE) It is a nice shorthand for the typical if-else statement: if (COND) { RESULT = (STATEMENT IF TRUE); } else { RESULT = (STATEMENT IF FALSE);

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.

What does =+ mean in C #?

1 vote. The += operator performs enhanced assignments. The value of the expression to the right of the operator is added to the value of the variable to the left of the operator, and the result replaces the value of the variable. For example …


1 Answers

It's bitwise negation. This means that it performs the binary NOT operator on every bit of a number. For example:

int x = 15; // Binary: 00000000 00000000 00000000 00001111
int y = ~x; // Binary: 11111111 11111111 11111111 11110000

When coupled with the & operator it is used for clearing bits. So, in your example it means that the last 3 bits of the result of var2+7 are set to zeroes.

As noted in the comments, it's also used to denote destructors, but that's not the case in your example.

like image 86
Vilx- Avatar answered Sep 18 '22 17:09

Vilx-