The tilde operator in C

C++COperators

C++ Problem Overview


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;
}

C++ Solutions


Solution 1 - C++

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

NOT 011100
  = 100011

Solution 2 - C++

~ is the bitwise NOT operator. It inverts the bits of the operand.

For example, if you have:

char b = 0xF0;  /* Bits are 11110000 */
char c = ~b;    /* Bits are 00001111 */

Solution 3 - C++

This is the bitwise NOT operator. It flips all the bits in a number: 100110 -> 011001

Solution 4 - C++

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

For example: ~0x0044 = 0xFFBB.

Solution 5 - C++

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

Solution 6 - C++

Tilde operator (~) also called bitwise NOT operator, performs one's complement of any binary number as argument. If the operand to NOT is decimal number then it convert it as binary and perform's one's complement operation.

To calculate one's complement simply invert all the digit [0-->1] and [1-->0] Ex : 0101 = 5; ~(0101) = 1010. Use of tilde operator :

  1. It is used in masking operation , Masking means setting and resetting the values inside any register . for ex :

    char mask ; mask = 1 << 5 ;

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

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

This is called Masking of bits. 2.To find 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 same as 00001010

My observation :For the maximum range of any data type , one's complement provide the negative value decreased by 1 to any corresponding value. ex:
~1 --------> -2
~2---------> -3
and so on... I will show you this observation using little code snippet

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

Note : This is valid only for the range of data type. means for int data type this rule will be applicable only for the value of range[-2,147,483,648 to 2,147,483,647].
Thankyou .....May this help you

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul MantaView Question on Stackoverflow
Solution 1 - C++GWWView Answer on Stackoverflow
Solution 2 - C++dlevView Answer on Stackoverflow
Solution 3 - C++immortalView Answer on Stackoverflow
Solution 4 - C++CedekasmeView Answer on Stackoverflow
Solution 5 - C++Sander De DyckerView Answer on Stackoverflow
Solution 6 - C++MinionView Answer on Stackoverflow