2

I was reading some tutorial about openGL in qt. One of the mouse event slot has this code in it:

if (event->buttons() & Qt::LeftButton) {    
    rotationX += 180 * dy;
    rotationY += 180 * dx;
    updateGL();
}

what does the & operator do in the if statement? is it exactly the same as == ?

5 Answers 5

5

It is not the same as ==. It is bitwise AND operator. What the expression does is that it takes the return value from event->buttons() and bitwise AND's it with the value represented by Qt::LeftButton. If the resulting value is non-zero the block is being executed.

In essence, it checks if the button specified by Qt::LeftButton is held down.

The reason why the bitwise AND operator is used here is something called a bitmask. What it means is that the return value of event->buttons() is just a value which has it's bits represent different kinds of states. What is done with the &-operator here is that it checks if certain bits(denoted by Qt::LeftButton) are being set(1) or unset(0) in the value returned by event->buttons(). The return value is zero if no tested bit is set, and non-zero, if at least one of the tested bits is set.

More details of how bitwise operations work can be found here: Wikipedia article about Bitwise operations

2
  • 3
    As a practical example of where this is different from ==, I imagine there is also Qt::RightButton, Qt::MiddleButton, etc. Those would all have different bitwise combinable values. Qt::AnyMouseButton may equal Qt::RightButton | Qt::LeftButton | Qt::MiddleButton. You can do the bitmask operations to see whether any combination of these are set. Or in the original question's example, in case there were multiple buttons associated with the event you can check that LeftButton is down, even if the event also indicated that RightButton was also being pressed.
    – Joe Castro
    Commented Jun 22, 2012 at 21:59
  • Thank you so much! It's very informative! So if I use == instead of & here, will it do the same job as & or just return false since bitwise they are not the same? Sorry never mind I got it~ Commented Jun 23, 2012 at 0:08
3

That will test that the value event=>buttons() has the bit Qt::LeftButton.

The result would be a 0, if it did not have that bit. and a Qt::LeftButton if it did include that bit.

it is a comparison to check the existence of a flag or bit value on a number

0001 == 1
0010 == 2
0011 == 3

1 & 2 == 0 (false)
1 & 3 == 1 (true)
2 & 3 == 2 (true)

essentially it is a match across the two values for their bit values.

  (0001) 
& (0010)
---------
  (0000) //Neither have the same bit

  (0011)
& (0010)
---------
  (0010) //both have bit 2

  (0101)
& (0110)
---------
  (0100) // Both have the 3rd bit

  (0111)
& (0110)
---------
  (0110) // Both have the 2nd and 3rd bit

Boolean values in C languages are 0 for false. and anything non zero is true.

This proved that the 1st and 2nd bit are available in the number 3. however 1 and 2 do not have matching bits.

Look into bitwise operators. to get a better understanding.

http://en.wikipedia.org/wiki/Bitwise_operation

0

It's the bitwise AND operator.

  0011
& 0101
------
  0001
0

event->buttons() presumably returns a value that's a combination of bits where each bit represents one button. Qt::LeftButton is going to be a value that probably has just a single bit set in the position that corresponds to the "left button". Using bitwise AND (&) here ANDs the individual bits of those two values, and the condition will be considered true if the result is non-zero.

Since there's only one bit in Qt::LeftButton, the only way to get a non-zero value is if event->buttons() has the same bit set. (It may have other bits set too, but those go away when they're ANDed with the zero bits in those positions in Qt::LeftButton. Effectively, then, the expression means "true if and only if event->buttons() includes the bit represented by Qt::LeftButton".

0

This is the bitwise AND operator. The better question is: What's the job of the AND operator here? Why are we using such a "low-level" method here?

We got a set of flags here. The set of buttons held down is represented by event->buttons(). That means, it is the sum of all buttons held down. But every button is a unique power of two, so the sum of all buttons held down is a set of bits in an integer. I hope you understand this, as this is an essential part of how we can represent simple sets of limited elements in C / C++.

The point is, every bit in the so-called bitset represents one element in the set. So does every element have a unique number, which we have to be able to test against the bitset (if it is contained in the bitset).

If you want to test whether or not the left button was held down during the event, you have to check whether or not the bit is set in the bitset. This is done using the bitwise AND operator, as this combines all the bits of the operands bit for bit using the boolean AND operation. As you should know, the AND oepration returns true if and only if both of the input bits are true. So the bitwise AND operation works as a mask for the input bits. The right operand "filters out" the bits of the left operand which are present in the right operand.

As the if condition is interpreted as true if and only if the value is unequal zero, this equals the question whether or not the bits of the right operand also appear in the left operand. In this concrete scenario, this means: Is the value Qt::LeftButton bitwise contained in the value event->buttons(), or: is the bit represented by Qt::LeftButton contained in the bitset represented by event->button()?

Or simply: Is the left button held down?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.