473,583 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking a bitwise enumeration

Suppose I have a few Keys objects:

Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J

I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.

P.
Nov 16 '05 #1
8 6929
if( ((int)k2 & (int)Keys.V) != 0 ) {
// k2 contains Keys.V
}

hope that helps

Chris

"Paul E Collins" <fi************ ******@CL4.org> wrote in message
news:cb******** **@sparta.btint ernet.com...
Suppose I have a few Keys objects:

Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J

I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.

P.

Nov 16 '05 #2
Try this

if((k1 & Keys.V)== Keys.V)
MessageBox.Show ("key found in k1");

if((k2 & Keys.V) == Keys.V)
MessageBox.Show ("key found in k2");

if((k3 & Keys.V) == Keys.V)
MessageBox.Show ("key found in k3");

--
Shak
(Houston)
"Paul E Collins" <fi************ ******@CL4.org> wrote in message
news:cb******** **@sparta.btint ernet.com...
Suppose I have a few Keys objects:

Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J

I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.

P.

Nov 16 '05 #3
Paul E Collins wrote:
Suppose I have a few Keys objects:

Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J

I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.


Use the Keys.KeyCode enum value to isolate only the keycode portion of
the value (ie., get rid of any modifier bits):

bool isVKey = ((k2 & Keys.KeyCode) == Keys.V);

--
mikeb
Nov 16 '05 #4
Hi Paul,

Be adviced that you may check for Key modifiers like Control, Shiftm and Alt
because they are specific bits, but you cannot check for keys in any cases
becuse they are just a numbers. The are not meant to be check like that. So
bitwise operations won't do you have to go with the not so cool looking
*if* or *switch* and check KeyEventArgs.Ke yCode property in order to get rid
of the modifiers.

Just to demosntrate my point
Keys key = Keys.K;
Console.WriteLi ne((key & Keys.J)!=0);
Console.WriteLi ne((key & Keys.K)!=0);

both print *true*

that is because
Key.J = 74 -> 1001010
Key.K = 75 -> 1001011

See they differ only in one bit K has all the (1) bits of J so if you have K
there is now way via bitwise operations to make the difference

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Paul E Collins" <fi************ ******@CL4.org> wrote in message
news:cb******** **@sparta.btint ernet.com...
Suppose I have a few Keys objects:

Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J

I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.

P.

Nov 16 '05 #5
Hi Stoitcho,

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
Hi Paul,

Be adviced that you may check for Key modifiers like Control, Shiftm and Alt because they are specific bits, but you cannot check for keys in any cases
becuse they are just a numbers. The are not meant to be check like that. So bitwise operations won't do you have to go with the not so cool looking
*if* or *switch* and check KeyEventArgs.Ke yCode property in order to get rid of the modifiers.

Just to demosntrate my point
Keys key = Keys.K;
Console.WriteLi ne((key & Keys.J)!=0);
Console.WriteLi ne((key & Keys.K)!=0);

both print *true*

that is because
Key.J = 74 -> 1001010
Key.K = 75 -> 1001011

See they differ only in one bit K has all the (1) bits of J so if you have K there is now way via bitwise operations to make the difference


You can do this:

Keys key = Keys.K;
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.J); // prints false
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.K); // prints true

Regards,
Daniel
Nov 16 '05 #6
That's right,
this is the same as
Keys key = Keys.K;
Console.WriteLi ne(key.KeyCode == Keys.J); // prints false
Console.WriteLi ne(key.KeyCode == Keys.K); // prints true

This is exactly what I said. One cannot use bitwise operations to find out
the key pressed, but bitwise operations can be used to find the modifier
key.

--

Stoitcho Goutsev (100) [C# MVP]
"Daniel Pratt" <ko************ ******@hotmail. com> wrote in message
news:e%******** ********@tk2msf tngp13.phx.gbl. ..
Hi Stoitcho,

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
Hi Paul,

Be adviced that you may check for Key modifiers like Control, Shiftm and Alt
because they are specific bits, but you cannot check for keys in any cases becuse they are just a numbers. The are not meant to be check like that.

So
bitwise operations won't do you have to go with the not so cool looking
*if* or *switch* and check KeyEventArgs.Ke yCode property in order to get

rid
of the modifiers.

Just to demosntrate my point
Keys key = Keys.K;
Console.WriteLi ne((key & Keys.J)!=0);
Console.WriteLi ne((key & Keys.K)!=0);

both print *true*

that is because
Key.J = 74 -> 1001010
Key.K = 75 -> 1001011

See they differ only in one bit K has all the (1) bits of J so if you

have K
there is now way via bitwise operations to make the difference


You can do this:

Keys key = Keys.K;
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.J); // prints false
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.K); // prints true

Regards,
Daniel

Nov 16 '05 #7
Oops. I meant my example to look more like this:

Keys key = Keys.K | Keys.Shift;
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.J); // prints false
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.K); // prints true

I'm not disagreeing with you, btw. I think it comes down to semantics. I am
using bitwise operations to find out the key pressed, but your point is well
taken.

Regards,
Daniel

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
That's right,
this is the same as
Keys key = Keys.K;
Console.WriteLi ne(key.KeyCode == Keys.J); // prints false
Console.WriteLi ne(key.KeyCode == Keys.K); // prints true

This is exactly what I said. One cannot use bitwise operations to find out
the key pressed, but bitwise operations can be used to find the modifier
key.

--

Stoitcho Goutsev (100) [C# MVP]
"Daniel Pratt" <ko************ ******@hotmail. com> wrote in message
news:e%******** ********@tk2msf tngp13.phx.gbl. ..

You can do this:

Keys key = Keys.K;
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.J); // prints false
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.K); // prints true

Regards,
Daniel

Nov 16 '05 #8
Actually you don't discover the key using a bitwise operation. You use &
just to remove the modifers from the key value.

If there were no modifiers the only thing that you would do is

Keys key = Keys.K;
Console.WriteLi ne(key == Keys.J); // prints false
Console.WriteLi ne(key ==Keys.K); // prints true
there is no bitwise operation.

What I understand by "using bitwise operations for..." is
if we could do (but we can't)

key = Keys.K | Keys.J

how would I find whether K has been pressed?

if((key & Keys.K) != 0)
{
K is pressed
}

But because key codes are not a bit flags we can not actually do that.

Only thing that we can use bitwise operations is to remove modifer keys as
you suggested, but less verbose, I believe, is to use KeyCode that what it's
for

--

Stoitcho Goutsev (100) [C# MVP]
"Daniel Pratt" <ko************ ******@hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Oops. I meant my example to look more like this:

Keys key = Keys.K | Keys.Shift;
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.J); // prints false
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.K); // prints true

I'm not disagreeing with you, btw. I think it comes down to semantics. I am using bitwise operations to find out the key pressed, but your point is well taken.

Regards,
Daniel

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
That's right,
this is the same as
Keys key = Keys.K;
Console.WriteLi ne(key.KeyCode == Keys.J); // prints false
Console.WriteLi ne(key.KeyCode == Keys.K); // prints true

This is exactly what I said. One cannot use bitwise operations to find out the key pressed, but bitwise operations can be used to find the modifier
key.

--

Stoitcho Goutsev (100) [C# MVP]
"Daniel Pratt" <ko************ ******@hotmail. com> wrote in message
news:e%******** ********@tk2msf tngp13.phx.gbl. ..

You can do this:

Keys key = Keys.K;
Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.J); // prints false Console.WriteLi ne((key & ~Keys.Modifiers )==Keys.K); // prints true

Regards,
Daniel


Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
12625
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="red"/> <xsd:enumeration value="yellow"/> <xsd:enumeration value="blue"/> </xsd:simpleType>
18
2357
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if there was a bit more typechecking ?
2
3456
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic...
3
12994
by: Anders Borum | last post by:
Hello! Consider the following enumeration used to combine several of the values. I am trying to figure out what the best solution is to check for the presence of a value using bitwise comparisons. I'm able to get if statements to work, but am unsure if switch statemens allow this. Maybe it's just me on a friday, but if you've got a better...
8
1693
by: xanthviper | last post by:
Hey, I'm trying to do what I think is pretty basic here, but I am missing something. Hopefully someone can point me in the right direction. Essentially I have an enumeration set up as the following: public enum DocumentFlags {
3
2219
by: Steve Long | last post by:
this is probably trivial, but, I just am not sure how to do it. Say I have an argument for a routine that is an enumeration such as moNone 0 'None moLong 3 'Long moDouble 5 'Double moDate 7 'Date moString 8 'String This enum is defined in some other assymbly btw.
20
2063
by: cozzzy | last post by:
Hello, I have an unsigned int variable, which is really a set of bits. So what I need, is to check specific bit state: on/off (0 or 1). For example, I need to know is the 16-th bit is on. How can I do it? Thanks
5
5773
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not...
17
2351
by: zirconx | last post by:
I'm trying to understand how the bitwise AND can be used. I've read about what it does but am having trouble applying it practically. I'm working on a system that somebody else wrote and they make use of a MODE flag that gets passed in. They then compare the mode flag against a hard coded value using bitwise AND, and then show or don't show...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.