I've seen the tilde operator used in the ELF hashing algorithm, and I'm curious what it does. (The code is from Eternally Confused.)
unsigned elf_hash ( void *key, int len ) { unsigned char *p = key; unsigned h = 0, g; int i; for ( i = 0; i < len; i++ ) { h = ( h << 4 ) + p[i]; g = h & 0xf0000000L; if ( g != 0 ) h ^= g >> 24; h &= ~g; } return h; }
A tilde is a typographical symbol that resembles a wavy line (~). In English, it has no accepted usage in formal writing, but it may occasionally be used for a few different reasons in informal writing. This symbol is also used in math, computer programming, and to form certain letters in Spanish and Portuguese.
The ~ operator in C++ (and other C-like languages like C and Java) performs a bitwise NOT operation - all the 1 bits in the operand are set to 0 and all the 0 bits in the operand are set to 1. In other words, it creates the complement of the original number.
The bitwise NOT operator in C++ is the tilde character ~ . Unlike & and |, the bitwise NOT operator is applied to a single operand to its right. Bitwise NOT changes each bit to its opposite: 0 becomes 1, and 1 becomes 0.
The Tilde ( ~ ) performs a bitwise complement of a numerical value in Java.
The ~
operator is bitwise NOT, it inverts the bits in a binary number:
NOT 011100 = 100011
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With