简体   繁体   English

C中的波浪号运算符

[英]The tilde operator in C

I've seen the tilde operator used in the ELF hashing algorithm, and I'm curious what it does. 我已经看到了ELF哈希算法中使用的波浪号运算符,我很好奇它是做什么的。 (The code is from Eternally Confused .) (代码来自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;
}

The ~ operator is bitwise NOT , it inverts the bits in a binary number: ~运算符是按位NOT ,它以二进制数反转位:

NOT 011100
  = 100011

~ is the bitwise NOT operator. ~是按位NOT运算符。 It inverts the bits of the operand. 它反转操作数的位。

For example, if you have: 例如,如果您有:

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

This is the bitwise NOT operator. 这是按位NOT运算符。 It flips all the bits in a number: 100110 -> 011001 它会翻转一个数字中的所有位:100110 - > 011001

It is the bitwise NOT operator. 它是按位NOT运算符。 It inverts all bits in an integer value. 它将整数值中的所有位反转。

The tilde character is used as an operator to invert all bits of an integer (bitwise NOT). 波形符号用作运算符以反转整数的所有位(按位NOT)。

For example: ~0x0044 = 0xFFBB . 例如: ~0x0044 = 0xFFBB

Tilde operator (~) also called bitwise NOT operator, performs one's complement of any binary number as argument. Tilde运算符(〜) 称为按位NOT运算符,执行任何二进制数的一个补码作为参数。 If the operand to NOT is decimal number then it convert it as binary and perform's one's complement operation. 如果NOT的操作数是十进制数,那么它将它转换为二进制并执行一次补码操作。

To calculate one's complement simply invert all the digit [0-->1] and [1-->0] Ex : 0101 = 5; 要计算一个补码,只需将所有数字反转[0 - > 1]和[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 . 〜(0101)= 1010.使用波浪号运算符:1。用于屏蔽操作,屏蔽表示设置和重置任何寄存器内的值。 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 . 它将掩码设置为二进制值10000,并且此掩码可用于检查其他变量中存在的位值。

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. 2.使用屏蔽属性查找任意数字的二进制等效项。

#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 输出:十进制10与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. 我的观察 :对于任何数据类型的最大范围,一个补码提供的负值减少1到任何相应的值。 ex: 例如:
~1 --------> -2 ~1 --------> -2
~2---------> -3 ~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]. 对于int数据类型的含义,此规则仅适用于范围[-2,147,483,648到2,147,483,647]的值。
Thankyou .....May this help you 谢谢.....这可以帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM