Tilde operator in C

I saw the tilde operator used in the ELF hash algorithm, and I'm curious what it does. (Code 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; } 
+78
c ++ c operators
Aug 26 '11 at 15:53
source share
6 answers

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

 NOT 011100 = 100011 
+103
Aug 26 '11 at 15:54
source share

~ is a bitwise NOT operator. It inverts the operand bit.

For example, if you have:

 char b = 0xF0; /* Bits are 11110000 */ char c = ~b; /* Bits are 00001111 */ 
+38
Aug 26 '11 at 15:54
source share

This is the bitwise NOT operator. It flips all bits in a number: 100110 β†’ 011001

+10
Aug 26 '11 at 15:54
source share

This is the bitwise NOT operator. It inverts all bits in an integer value.

+7
Aug 26 '11 at 15:54
source share

The tilde character is used as an operator to invert all bits of an integer (bitwise NOT).

For example: ~0x0044 = 0xFFBB .

+7
Aug 26 '11 at 15:57
source share

The Tilda operator (~) is also called the bitwise NOT operator; it performs one complement of any binary number as an argument. If the NOT operand is a decimal number, it converts it as binary and performs one complement operation.

To calculate one complement, simply invert all the digits [0 β†’ 1] and [1 β†’ 0] Example: 0101 = 5; ~ (0101) = 1010. Using the tilde operator: 1. It is used for masking, Masking means setting and resetting the values ​​inside any register. for ex:

 char mask ; mask = 1 << 5 ; 

It will set the mask to a binary value of 10000, and this mask can be used to check the value of a bit present inside another variable.

 int a = 4; int k = a&mask ; if the 5th bit is 1 , then k=1 otherwise k=0. 

This is called a disguise bit. 2. Find the binary equivalent of any number using masking properties.

 #include<stdio.h> void equi_bits(unsigned char); int main() { unsigned char num = 10 ; printf("\nDecimal %d is same as binary ", num); equi_bits(num); return 0; } void equi_bits(unsigned char n) { int i ; unsigned char j , k ,mask ; for( i = 7 ; i >= 0 ; i--) { j=i; mask = 1 << j; k = n&mask ; // Masking k==0?printf("0"):printf("1"); } } 

Output: Decimal 10 is the same as 00001010

My observations . For the maximum range of any data type, one padding provides a negative value reduced by 1 to any corresponding value. ex:
~ 1 --------> -2
~ 2 ---------> -3
and so on ... I will show you this observation using a small piece of code

 #include<stdio.h> int main() { int a , b; a=10; b=~a; // b-----> -11 printf("%d\n",a+~b+1);// equivalent to ab return 0; } Output: 0 

Note. This is only valid for a range of data types. for an int data type, this rule will apply only for the range value [-2,147,483,648 to 2,147,483,647].
Thanks ... Maybe this will help you.

0
Mar 06 '17 at 18:35
source share



All Articles