0

I am getting unexpected results (likely due to my misuse or misunderstanding of how a bitwise (&) expression is supposed to work). I have the following code:

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, 

I am looking for an explanation as to where my logic is failing. My end expression I am looking to accomplish is:

uint xpos = 150;
uint ypos = 150;
uint val = ((xpos) & ((ypos) << 16))); // result is 0, should be 9830550‬ as above)

Of course

((xpos) + ((ypos) << 16))); // Would work properly

However all examples I have seen (for purposes of mouse position (POINT) locations all have shown '&' in the expression)

Aaron Murray
  • 1,920
  • 3
  • 22
  • 38
  • 2
    `&` will return the bits that are set in both values. 150 and 9830400 do not have any common bits, so you get 0. You might be looking for `|`, which will return bits that are set on *either* value. Note that is not the same as addition... for ex. `1 | 1` will equal 1, as the same bits are set in both values. – Glorin Oakenfoot Apr 20 '16 at 19:19
  • 2
    You have bitwise AND and bitwise OR confused. Try using `|` instead of `&`. – willaien Apr 20 '16 at 19:19
  • Ahh yes that makes sense. I understand, so then my confusion exists in the example (answer) provided in http://stackoverflow.com/questions/10279812/simulate-click-into-a-hidden-window. Showing: PostMessage ( hwndWindowTarget, WM_RBUTTONUP, 0, (pt.x ) & (( pt.y) << 16) ); // This should technically be incorrect then – Aaron Murray Apr 20 '16 at 19:23

1 Answers1

5

Here is the binary representation of your two numbers:

00000000 00000000 00000000 10010110
00000000 10010110 00000000 00000000

So it should hopefully be pretty clear that there won't be a bit that is 1 in both numbers, so when you do a bitwise AND of these numbers it comes out to 0.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Thank you Servy, after I read the comment from @Glorin Oakenfoot in the question (as well as your answer that made sense I should have seen that right away), Any chance you can explain why the other stack overflow (from my comment) shows usage of & then (of which there are other similar questions/answers using & in the equation as well) – Aaron Murray Apr 20 '16 at 19:38
  • @AaronMurray They might have different data, in which ANDing the values makes sense, or it could be a bug. – Servy Apr 20 '16 at 19:41
  • Yeah this all started because I was using the expression (from the other stack overflow question)and the mouse was showing at position 0, 0. So I was breaking it down to see why it just kept returning 0 and not what I was expecting. So I am going to go with a bug, (and I will re-review the MSDN documentation to confirm the actual representation of the data), However, I marked this as correct because it does answer the original question as asked (and which I knew but just got distracted by the 'bug' and thus confused) – Aaron Murray Apr 20 '16 at 19:47