469,646 Members | 1,131 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

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 6697
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.btinternet.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.btinternet.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.KeyCode property in order to get rid
of the modifiers.

Just to demosntrate my point
Keys key = Keys.K;
Console.WriteLine((key & Keys.J)!=0);
Console.WriteLine((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.btinternet.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***************@tk2msftngp13.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.KeyCode property in order to get rid of the modifiers.

Just to demosntrate my point
Keys key = Keys.K;
Console.WriteLine((key & Keys.J)!=0);
Console.WriteLine((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.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((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.WriteLine(key.KeyCode == Keys.J); // prints false
Console.WriteLine(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%****************@tk2msftngp13.phx.gbl...
Hi Stoitcho,

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2***************@tk2msftngp13.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.KeyCode property in order to get

rid
of the modifiers.

Just to demosntrate my point
Keys key = Keys.K;
Console.WriteLine((key & Keys.J)!=0);
Console.WriteLine((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.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((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.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((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****************@TK2MSFTNGP11.phx.gbl...
That's right,
this is the same as
Keys key = Keys.K;
Console.WriteLine(key.KeyCode == Keys.J); // prints false
Console.WriteLine(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%****************@tk2msftngp13.phx.gbl...

You can do this:

Keys key = Keys.K;
Console.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((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.WriteLine(key == Keys.J); // prints false
Console.WriteLine(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****************@TK2MSFTNGP10.phx.gbl...
Oops. I meant my example to look more like this:

Keys key = Keys.K | Keys.Shift;
Console.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((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****************@TK2MSFTNGP11.phx.gbl...
That's right,
this is the same as
Keys key = Keys.K;
Console.WriteLine(key.KeyCode == Keys.J); // prints false
Console.WriteLine(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%****************@tk2msftngp13.phx.gbl...

You can do this:

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

Regards,
Daniel


Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Justin Wright | last post: by
18 posts views Thread by Nebula | last post: by
2 posts views Thread by Steve Summit | last post: by
8 posts views Thread by xanthviper | last post: by
20 posts views Thread by cozzzy | last post: by
5 posts views Thread by noridotjabi | last post: by
17 posts views Thread by zirconx | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.