3

I need to get the 30th bit of the lParam param passed with the WM_KEYDOWN message. This bit as written here allows me to know if the key was pressed before. Is this code right to get it?

(lParam >> 30) & 1
Stefano
  • 3,213
  • 9
  • 60
  • 101

1 Answers1

9

I would just use lParam & 0x40000000. If that's non-zero, then b30 was set (I consider that the thirty first bit of the thirty two, by the way). And there's more likelihood that it will be a {logical-and, compare} operation rather than {shift, logical-and, compare}.

Mind you, there's a good chance that a decent compiler would generate the more efficient code anyway even if you used (lParam >> 30) & 1 but why take the risk?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • @paxdiablo are you sure about this? I'm getting a strange behaviour with your code. I should check and set a boolean variable to true only if the key has been released before the message has been sent. To do so, I should check if the 30th bit is 1 (key is down before the message is sent) – Stefano May 30 '11 at 13:10
  • @Frerich, actually it's the 15th bit but I see where you're coming from - a silly mistake on my part which I've now fixed. – paxdiablo May 30 '11 at 13:10
  • @Stefano, `b30` is the 31st bit because the bits range from `b0` (the first) to `b31` (the thirty-second). That's just a slight difference in naming because, despite what tech-heads will try to convince you of, first still means first :-) `0x400000000` is binary `0100 0000 0000 0000 0000 0000 0000 0000`, which is the mask you're looking for. The link you posted uses bit positions (0-31) rather than `first` thru `thirty-second`. – paxdiablo May 30 '11 at 13:20
  • @paxdiablo: I'd like to revoke my downvote now that you fixed the answer, but it seems my vote is "locked in" until the answer is edited. :-/ – Frerich Raabe May 30 '11 at 13:21
  • @Frerich, I normally do a "phantom" edit when I want to do that (something that doesn't change the essence of the answer but still gets around SO's "guidelines"). You should be able to do it now if you wish. Or, seriously, you can just not worry about it. 2 rep points isn't going to change my life :-) and I later votes on my improved answer usually reverse the effect anyway. So no hard feelings if you don't get around to it. – paxdiablo May 30 '11 at 13:23
  • @paxdiablo thank you, I voted up and marked your question as the solution ;D – Stefano May 30 '11 at 13:27