.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification