[英]C++ operator equal to C#
This code is C++.这段代码是 C++。 I can't convert it to C#.
我无法将其转换为 C#。
The main problem is !(value & 0xf)
.主要问题是
!(value & 0xf)
。 I don't know what it does!!不知道有什么作用!!
I don't know what operator !(int)
does in C++我不知道运算符
!(int)
在 C++ 中做什么
int GetBitPosition(uint8_t value)
{
const int i4 = !(value & 0xf) << 2;
value >>= i4;
const int i2 = !(value & 0x3) << 1;
value >>= i2;
const int i1 = !(value & 0x1);
const int i0 = (value >> i1) & 1 ? 0 : -8;
return i4 + i2 + i1 + i0;
}
!
is the not
operator in C++.是 C++ 中的
not
运算符。 According to this answer :根据这个答案:
Yes.
是的。 For integral types, !
对于整数类型,! returns true if the operand is zero, and false otherwise.
如果操作数为零则返回真,否则返回假。
So !b here just means b == 0.
所以 !b 在这里只是意味着 b == 0。
In other words, if C# doesn't have an equivalent operator (which I don't understand why it wouldn't), you can, in theory, use the C# equivalent of the following instead:换句话说,如果 C# 没有等效的运算符(我不明白为什么它没有),理论上您可以使用 C# 等效的以下内容:
((value & 0xf) == 0) // parenthesis for readabillity
int GetBitPosition(int value)
{
int i4 = ((value & 0xf) == 0 ? 1 : 0) << 2;
value >>= i4;
int i2 = ((value & 0x3) == 0 ? 1 : 0) << 1;
value >>= i2;
int i1 = (value & 0x1) == 0 ? 1 : 0 ;
int i0 = ((value >> i1) & 1 ) == 1 ? 0 : -8;
return i4 + i2 + i1 + i0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.