Mohammad
Mohammad

Reputation: 153

What does this statement mean in C#?

What does if ((a & b) == b) mean in the following code block?

if ((e.Modifiers & Keys.Shift) == Keys.Shift)
{
    lbl.Text += "\n" + "Shift was held down.";
}

Why is it not like this?

if (e.Modifiers == Keys.Shift)
{
    lbl.Text += "\n" + "Shift was held down.";
}

Upvotes: 11

Views: 640

Answers (8)

cuongle
cuongle

Reputation: 75306

If you take a look Keys enum, this is flag enum with [FlagsAttribute] attribute.

Use the FlagsAttribute custom attribute for an enumeration only if a bitwise operation (AND, OR, EXCLUSIVE OR) is to be performed on a numeric value.

Define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. This means the individual flags in combined enumeration constants do not overlap.

So e.Modifiers might be a combination of more than one enum:

e.Modifiers = Keys.Shift | Keys.Cancel | Keys.Enter

Just very simple assumption to explain the concept:

Keys.Shift  : 001 (1)
Keys.Cancel : 010 (2)
Keys.Enter  : 100 (4)

So:

e.Modifiers = Keys.Shift | Keys.Cancel | Keys.Enter equal 001 | 010 | 100 = 111

And the condition:

    e.Modifiers & Keys.Shift equal 111 & 001 = 001

it means:

 e.Modifiers & Keys.Shift == Keys.Shift

if e.Modifiers does not contains Keys.Shift:

e.Modifiers = Keys.Cancel | Keys.Enter (110)

So the result will be:

e.Modifiers & Keys.Shift equals 110 & 001 = 000 (is not Keys.Shift)

To sump up, this condition checks whether e.Modifiers contains Keys.Shift or not

Upvotes: 17

Steve Konves
Steve Konves

Reputation: 2668

A single ampersand (&) performs a bit-wise AND operation; a double ampersand (&&) performs a boolean AND operation.

A bit-wise and performs an AND operation on each bit of both arguments (hence it is called "bit-wise"). Thus the output of a bit-wise AND operation (or any bit-wise operation) will not be a boolean value. Here are some examples of bit-wise AND operations:

1001 & 0001 = 0001
1101 & 1111 = 1101

A boolean AND operates on two boolean values and returns a boolean:

true && true = true
false && true = false

Short Circuiting
A boolean AND operation (&&) can also be performed on two expressions which return a boolean value:

int a = 5;
int b = 10;
bool result = (a < 3) && (b > 3);
// result will be false;

Because the first expression (a < 3) evaluates to false, the result cannot possibly be true because BOTH expressions have to evaluate to true in order for the result to be true. Because of this, the second expression will not even be evaluated. This is called "short circuiting". However, with a bit-wise AND operation, both expressions need to be evaluated before the operation is performed. Thus in the majority of situations where you just want to determine if two things are true (boolean) the boolean AND (&&) will be the best option.

In your example, the code is comparing individual bits within e.Modifiers with individual bits in Keys.Shift. Neither argument represents a boolean value and thus the operation is bit-wise (&) rather than boolean (&&).

Upvotes: 3

Xaruth
Xaruth

Reputation: 4104

It's boolean logic (& = "bitwise and"). You check if variable contain a value. It's like a filter.

Exemple :

a   -> 00110011
b   -> 00000011
a&b -> 00000011

In your code

if ((e.Modifiers & Keys.Shift) == Keys.Shift)

that checks Keys.Shift is contained in e.Modifiers.

Upvotes: 5

Johni
Johni

Reputation: 2961

Part 1

It's the logic AND operator.

It is used when multiple flags should be settable in one for example Integer:

a for example is 3 which means 00000011 in binary notation. b for example is 2 which means 00000010 in binary notation.

When you want to check if a has the flag (the second bit from right) which b represents set you use the AND operator:

a & b = 00000010

And when this equals b (or is > 0) you know that the flag is set.

Part 2

The equal operator can also be used for the case you want to check if "Keys.Shift" is the only "modifier" key and no other one is pressed. When you use the first code other "modifier" keys could be pressed, too and the if condition will still be true.

Upvotes: 3

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241475

A single ampersand refers to the bitwise AND operator. When used in conjunction with an enum that has the [Flags] attribute on it, which the Keys enum does, it is used as you showed to determine if one of the bits of that enum is set or not.

There could be more than one modifier key pressed simultaneously, so that's why it is used instead of a direct comparison.

You can read more about enum flags here. Scroll down to the subsection titled "Enumeration Types as Bit Flags". You'll see a sample that is quite similar to this.

Upvotes: 4

Matt Burland
Matt Burland

Reputation: 45135

& is the bitwise AND operator. What it is doing is Keys is a flag enum, the values in Keys can be a bitwise combination of several values. So to test for any particular value you first AND your value with the value you want to test and then compare it to the value you want to test.

For example, you might have both the shift and the ctrl key held down, so the value in e.Modifier will be a bitwise combination of Keys.Shift and Keys.Ctrl. So this:

e.Modifier == Keys.Shift

Is false. Shift is held down, but Ctrl is also held down. If you want to know if Shift is held down regardless of what other keys might be held down, you need to first filter out all the other keys. This is easily done by using Keys.Shift as a filter:

(e.Modifier & Keys.Shift) == Keys.Shift

This will now be true if the shift key is held down regardless of what other keys might be pressed and false otherwise.

Upvotes: 3

JRoughan
JRoughan

Reputation: 1655

It's using a bitwise operation to query if a "flag" (bit) is set (equal to 1).

Probably best to read up on enums and bitwise operations at http://msdn.microsoft.com/en-us/library/vstudio/cc138362.aspx

Upvotes: 3

Bryan Hobbs
Bryan Hobbs

Reputation: 444

A single ampersand (&) is a bitwise AND, so it is basically adding the values of (a & b) and then testing for equality in (a & b) == b

So in your example, it is basically saying if the shift key is pressed (any key + shift) == shift.

Upvotes: 3

Related Questions