Aar*_*ray 0 c# expression bit-manipulation
我得到了意想不到的结果(可能是由于我误用或误解了按位(&)表达式应该如何工作).我有以下代码:
string sbin = "10010110"; // 150 = ??1001 0110??
uint ival = Convert.ToUInt32(sbin, 2);
ival.Dump(); // 150 Expected Correct
uint lval = ival << 16; // Shift 16 bits left
lval.Dump(); // 9830400 - Expected Correct
string slval = Convert.ToString(lval, 2);
slval.Dump(); // 0000 0000 1001 0110 0000 0000 0000 0000 - Expected Correct
uint lrval = lval & ival; // <--- This is where I am confused (while a '+' operator works, why does the & not return the proper result?)
// expecting 0000 0000 1001 0110 0000 0000 1001 0110 (aka ?9830550? (dec))
lrval.Dump(); // returns 0,
Run Code Online (Sandbox Code Playgroud)
我正在寻找关于我的逻辑失败的地方的解释.我想要完成的结束表达是:
uint xpos = 150;
uint ypos = 150;
uint val = ((xpos) & ((ypos) << 16))); // result is 0, should be 9830550? as above)
Run Code Online (Sandbox Code Playgroud)
当然
((xpos) + ((ypos) << 16))); // Would work properly
Run Code Online (Sandbox Code Playgroud)
但是我看到的所有例子(出于鼠标位置(POINT)位置的目的都在表达式中显示'&')
这是两个数字的二进制表示:
00000000 00000000 00000000 10010110 00000000 10010110 00000000 00000000
所以应该很清楚,1两个数字都不会有一点,所以当你对这些数字进行按位运算时,它就会出现0.