C 中的波形符运算符
我见过 ELF 哈希算法中使用的波形符运算符,我很好奇它的作用。 (代码来自来自《永恒的困惑》。)
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
~
运算符是按位NOT,它反转中的位一个二进制数:The
~
operator is bitwise NOT, it inverts the bits in a binary number:~
是按位 NOT 运算符。它反转操作数的位。例如,如果您有:
~
is the bitwise NOT operator. It inverts the bits of the operand.For example, if you have:
这是按位非运算符。
它翻转数字中的所有位:100110 -> 011001
This is the bitwise NOT operator.
It flips all the bits in a number: 100110 -> 011001
波形符用作运算符来反转整数的所有位(按位 NOT)。
例如:
~0x0044 = 0xFFBB
。The tilde character is used as an operator to invert all bits of an integer (bitwise NOT).
For example:
~0x0044 = 0xFFBB
.它是按位非运算符。它反转整数值中的所有位。
It is the bitwise NOT operator. It inverts all bits in an integer value.
波形符运算符 (~)也也称为按位 NOT 运算符,对任何二进制数作为参数执行补码。如果 NOT 的操作数是十进制数,则将其转换为二进制并执行补码运算。
要计算补码,只需反转所有数字 [0-->1] 和 [1-->0]
例如:0101 = 5; 〜(0101)= 1010。
波形符运算符的使用:
1.用于屏蔽操作,屏蔽是指对任意寄存器内部的值进行设置和重置。例如:
它将掩码设置为二进制值 10000,并且该掩码可用于检查其他变量中存在的位值。
这称为位屏蔽。
2.使用掩码属性查找任意数字的二进制等价物。
输出:十进制 10 与 00001010 相同
我的观察:对于任何数据类型的最大范围,补码提供负值减 1 到任何对应值。
例如:
~1 --------> -2
~2---------> -3
等等...我将使用很少的代码片段向您展示这一观察结果
注意:这仅对数据类型范围有效。意味着对于 int 数据类型,此规则仅适用于范围 [-2,147,483,648 到 2,147,483,647] 的值。
谢谢.....希望这对你有帮助
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 :
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 .
This is called Masking of bits.
2.To find binary equivalent of any number using masking properties.
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
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