~ (Tilde) is ASCII code point 126 (0x7E), Unicode U+007E, and a printable punctuation character. It's a small wave hovering above the baseline — in Spanish and Portuguese it nasalizes vowels and transforms the letter n into ñ, but in ASCII it stands alone, a diacritical mark with no letter beneath it. Unix shells gave the tilde its most iconic computing role: ~ expands to the current user's home directory, so cd ~ takes you home and ~/Documents is an absolute path in disguise. C and C-family languages use it as the bitwise NOT operator, flipping every bit in a value — ~0 yields all ones, which in two's complement representation equals -1. Python adopted the same convention. In destructors, C++ prefixes the class name with ~ (~Widget()), marking the method that tears down what the constructor built. Regular expressions in some flavors use =~ as the match operator. The trap is a numerical one: applying ~ to unsigned integers produces values that depend entirely on the width of the type, so ~0 as a uint8_t is 255, as a uint16_t is 65535, and assigning the result to a narrower type without a cast silently truncates it — a bug that unit tests with small values won't catch. The tilde entered ASCII as a standalone version of the combining diacritical mark, inheriting its shape from medieval scribal abbreviations where a small n written above a letter indicated a following nasal consonant. UTF-8 encodes it as a single byte 0x7E. Unicode classifies it as Sm (Symbol, Math) with the name TILDE. The combining tilde (U+0303) is the version that actually sits above a letter, and the fullwidth ~ (U+FF5E) common in CJK text is a wider glyph frequently encountered in Japanese URLs, where it causes encoding headaches because some legacy Japanese encodings mapped it to a different code point entirely.
unsigned mask = 0x0F;unsigned cleared = x & ~mask; // clear low 4 bitsint is_power2 = (n > 0) && ((n & (n - 1)) == 0); // bit trick
~ expands to your home directory in Unix shells: ~/Documents means /home/username/Documents. ~user expands to another user's home. This expansion is done by the shell, not the OS — so it works in Bash commands but not in most programming languages without explicit expansion.
In npm (Node.js), ~1.2.3 means 'compatible with patch releases' — it allows 1.2.x but not 1.3.0. This is more conservative than ^1.2.3 (which allows 1.x.x). In Composer (PHP), ~ has similar meaning. The tilde gives you bug fixes without risking breaking changes from minor version bumps.
~ flips every bit of a number: ~5 (binary 0101) becomes -6 (binary ...1010 in two's complement). The result is always -(n+1). A clever trick: ~~value truncates a float to an integer (faster than Math.floor but less readable). ~-1 equals 0, which is why ~array.indexOf(x) was used as a truthy check.
Tilde has no special meaning in standard regex — it matches a literal ~. However, some tools use it as a regex operator: in Perl, =~ applies a regex (if $str =~ /pattern/). In Apache configs, ~ flags a regex match in location directives. These are tool-specific, not part of the regex language itself.