Bitwise operator
-
Whats the difference between
if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
{
//Code}
and
if(e.Button == MouseButtons.Right)
//CodeThey both work the same I am unable to understand the use of the logical operator & and ALSO when I change the & with && it wont work Can anyone help
-
Whats the difference between
if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
{
//Code}
and
if(e.Button == MouseButtons.Right)
//CodeThey both work the same I am unable to understand the use of the logical operator & and ALSO when I change the & with && it wont work Can anyone help
-
Whats the difference between
if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
{
//Code}
and
if(e.Button == MouseButtons.Right)
//CodeThey both work the same I am unable to understand the use of the logical operator & and ALSO when I change the & with && it wont work Can anyone help
They don't actually work the same. If that event was raised with both right and left mouse buttons pressed (eg: e.Button = (MouseButtons.Right | MouseButtons.Left)) then the two snippets would be different. The difference is that the first one checks to see if a particular bit in a value is set and the second checks to see if the value is exactly equal to some other value. I assume that when you switch & with && you got a compiler error. The single operators & and | are used to perform bitwise operations. The double operators && and || are used to perform operations on boolean values.
-
Whats the difference between
if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
{
//Code}
and
if(e.Button == MouseButtons.Right)
//CodeThey both work the same I am unable to understand the use of the logical operator & and ALSO when I change the & with && it wont work Can anyone help
Hi,
humayunlalzad wrote:
the logical operator &
No, && would be a logical operator, & is a bitwise operator The following is similar to your code (just assume MouseButtons.Right=1), but may be easier to understand:
for(int i=int.MinValue; i<int.MaxValue; i++) {
if ((i & 1) == 1) Console.WriteLine(i.ToString()+" is an odd number");
if (i == 1) Console.WriteLine(i.ToString()+" equals 1");
}i & 1 ANDs the bits of i with the bits of 1 (which is all zeroes except for the rightmost bit); the result is either 1 or 0, and depends solely on the lowest bit of i. As a result, the former test checks only a single bit (and hits many times), the latter checks all bits in variable i (and hits just once). :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi,
humayunlalzad wrote:
the logical operator &
No, && would be a logical operator, & is a bitwise operator The following is similar to your code (just assume MouseButtons.Right=1), but may be easier to understand:
for(int i=int.MinValue; i<int.MaxValue; i++) {
if ((i & 1) == 1) Console.WriteLine(i.ToString()+" is an odd number");
if (i == 1) Console.WriteLine(i.ToString()+" equals 1");
}i & 1 ANDs the bits of i with the bits of 1 (which is all zeroes except for the rightmost bit); the result is either 1 or 0, and depends solely on the lowest bit of i. As a result, the former test checks only a single bit (and hits many times), the latter checks all bits in variable i (and hits just once). :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
I do understand the behaviour of bitwise operatores on Integers. But what I still dont understand is
e.Button
how is that an Integer, or what bits does it have or how do we know those bits. I assumed that MouseButton is an enum and the bitwise operator would work on its underlying byte or int value. And thanx for the help
-
I do understand the behaviour of bitwise operatores on Integers. But what I still dont understand is
e.Button
how is that an Integer, or what bits does it have or how do we know those bits. I assumed that MouseButton is an enum and the bitwise operator would work on its underlying byte or int value. And thanx for the help
Hi, MouseEventArgs.Button returns a MouseButtons Enumeration, which basically is an int with some bits set. I suggest you read up on enums, and the Flags attribute if you want to know the specific details. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi, MouseEventArgs.Button returns a MouseButtons Enumeration, which basically is an int with some bits set. I suggest you read up on enums, and the Flags attribute if you want to know the specific details. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
Thanx a lot everything is clear now. You were a great help.
-
Thanx a lot everything is clear now. You were a great help.