Bit Operations Question (easy but my brain can't handle it)
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Alex Maghen (VIP)
I have multiple Keys enumeration variables (they are, I believe, internally
ints). Let's say I have 3 variables created in C# is follows:

Keys K1 = Keys.S | Keys.Control;
Keys K2 = Keys.S | Keys.Shift | Keys.Control;
Keys K3 = Keys.S | Keys.Shift | Keys.Control | Keys.Alt;

Now, let's say, I want to have a function which, regardless of what Keys
variable I am passed, will return a Keys value WITHOUT any Keys.Control,
Keys.Alt, or Keys.Shift. In other words, for all of the variables above (K1,
K2, K3), the function would simply return Keys.S only. I know that doing this
requires some kind of bit-wise operation but I can't seem to figure it out.

Help?

Alex
Reply to this message...
 
    
Imran Koradia (VIP)
private Keys myfunction(k As Keys) {
return(k & !(Keys.Control | Keys.Shift | Keys.Alt));
}

I hope the syntax is correct - I come from VB. Anyway, just in case that was
wrong, here's the VB version which you'll need to convert to C# if the above
one is not correct:

Private Function MyFunction(ByVal k As Keys) As Keys
Return (k And Not (Keys.Control Or Keys.Shift Or Keys.Alt))
End Function

hope that helps..
Imran.

"Alex Maghen" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Bruno Jouhier [MVP] (VIP)
"Imran Koradia" <Click here to reveal e-mail address> a écrit dans le message de news:
Click here to reveal e-mail address...
[Original message clipped]

private Keys FilterKeys(Keys k) { return k & ~(Keys.Control | Keys.Shift |
Keys.Alt); }

[Original message clipped]

Reply to this message...
 
    
Imran Koradia (VIP)
ha! I had the feeling most likely I'd mess up that C# code. Thanks for
pointing that out.
Just out of curiosity - wouldn't the '!' operator work here?

Imran.

"Bruno Jouhier [MVP]" <Click here to reveal e-mail address> wrote in message
news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Imran Koradia <Click here to reveal e-mail address> wrote:
[Original message clipped]

No - ! is the boolean "not" operator. ~ is the bitwise compliment
operator.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Imran Koradia (VIP)
hmm. So I guess VB makes it a bit simpler since both the logical and bitwise
negation operations are done by the 'Not' keyword. Ofcourse, it could get
confusing sometimes as well.

Imran.

"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Robert Jordan
Jon Skeet [C# MVP] wrote:

[Original message clipped]

The animal it's the bitwise complEment operator.

bye
Rob
Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Robert Jordan <Click here to reveal e-mail address> wrote:
[Original message clipped]

Doh! Yes...

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
 
System.Windows.Forms.Keys




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com