What does this & operator mean here?

前端 未结 5 698
南方客
南方客 2020-12-12 00:32

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) {    
    rota         


        
5条回答
  •  星月不相逢
    2020-12-12 01:10

    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".

提交回复
热议问题