Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the tilde mean in an expression? [duplicate]

Tags:

syntax

c#

Possible Duplicate:
What is the tilde (~) in a C# enumeration?

I found the following bit of code on a this MSDN page.

(((Width * Planes * BitCount + 31) & ~31) / 8) * abs(Height) 

This does indeed compile in C# visual studio 2010. What exactly is the tilde "~" doing in front of the number 31? I've never seen this syntax in an expression before.

like image 814
Ultratrunks Avatar asked Nov 02 '11 20:11

Ultratrunks


People also ask

What is the tilde symbol used for?

In informal writing, a tilde is sometimes used before a number to mean “about” or “approximately.” For example, a sentence that reads I think my dog weighs ~20 pounds means I think my dog weighs about/around 20 pounds. This usage is likely based on the math usage of the tilde symbol to mean an approximate equivalency.

What is a double tilde?

Another approximation symbol is the double tilde ≈, meaning "approximately equal to". The tilde is also used to indicate congruence of shapes by placing it over an = symbol, thus ≅.

What does tilde mean in assembly?

Last updated: September 8, 2017. In JavaScript, the tilde ~ Bitwise NOT operator is commonly used right before an indexOf() to do a boolean check (truthy/falsy) on a string. On its own, indexOf() returns the index number of a String object passed in.

What is the squiggly Dash called?

It's called a tilde. Around the 12th century, Spanish scribes, in part to save paper, placed the tilde over a letter to indicate that it was doubled. As time passed, the mark was only used over the letter “n”; eventually, the ñ became an actual letter of the Spanish alphabet.


2 Answers

~ - bitwise NOT operator, basically invert bits

31 in binary format is 11111, so ~31 == 11111111111111111111111111100000, or 0xFFFFFFE0 hexadecimal.

like image 79
sll Avatar answered Sep 19 '22 03:09

sll


That is the bitwise complement operator, also known as bitwise negation.

like image 23
David Heffernan Avatar answered Sep 21 '22 03:09

David Heffernan