尝试在C#中的UInt16上执行按位不(〜)时出错 [英] Error when trying to perform a bitwise not (~) on a UInt16 in C#
本文介绍了尝试在C#中的UInt16上执行按位不(〜)时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
出于某种原因,我只是不了解(或看到)其工作原理:
For some reason, I am simply not understanding (or seeing) why this works:
UInt32 a = 0x000000FF;
a &= ~(UInt32)0x00000001;
但这不是:
UInt16 a = 0x00FF;
a &= ~(UInt16)0x0001;
它给出错误常量值-(某些数字)无法转换为UInt16".有人可以解释这是为什么以及如何解决该问题吗?
it gives the error 'constant value -(some number) cannot be converted to a UInt16'. Can someone explain why this is and how to work around the problem?
推荐答案
按位取反可将结果提升为int
,尽管进行了强制转换.您可以通过按位求和并将按位求反的结果降低到低16位来解决此问题,如下所示:~0x0001 & 0xFFFF
.
The bitwise negation promotes the result to a int
, despite the cast. You can overcome this by bitwise and-ing the result of the bitwise negation to the lower 16 bits, like this: ~0x0001 & 0xFFFF
.
这篇关于尝试在C#中的UInt16上执行按位不(〜)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文