Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The tilde operator in C

Tags:

c++

c

operators

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; } 
like image 985
Paul Manta Avatar asked Aug 26 '11 15:08

Paul Manta


People also ask

What is a tilde symbol?

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.

What does =~ mean in C?

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.

What is tilde in Bitwise operator?

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.

What is tilde operator in Java?

The Tilde ( ~ ) performs a bitwise complement of a numerical value in Java.


1 Answers

The ~ operator is bitwise NOT, it inverts the bits in a binary number:

NOT 011100   = 100011 
like image 192
GWW Avatar answered Oct 04 '22 20:10

GWW