funky looking conditional statement i think

Started by
17 comments, last by swiftcoder 10 years, 9 months ago

This should be a simple one. I am trying to rewrite this MouseCallback function. It is a Gluts MouseCallback function not that it matters i guess. I ran across a set of statements which im pretty sure are conditoinal statements. but i have never seen this kind of set up for a conditional statement before.


  mouseLClickButton |= (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN);
  mouseRClickButton |= (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN);
  mouseMClickButton |= (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN);

  mouseLClickButton &= !(button == GLUT_LEFT_BUTTON && state == GLUT_UP);
  mouseRClickButton &= !(button == GLUT_RIGHT_BUTTON && state == GLUT_UP);
  mouseMClickButton &= !(button == GLUT_MIDDLE_BUTTON && state == GLUT_UP);

can some one explain what the first set of operators are saying. |= and &=

im assuming that the first three are conditional statements for individual button presses and the second three are for multiple button presses. But i would just like an explanation of what exactly is going on here with the syntax. I have never seen anything like that.

mouseClickButton are all bools. im guessing that if the contitional statement on the right of the operator is tru then it makes that bool on the left tru but its just kind of confusing to me.

J-GREEN

Greenpanoply
Advertisement

This is interesting. I did a little digging and found the following. They are apparently called (bitwise OR assignment) and (bitwise AND assignment),

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Here are two more pages that I found using the 'english' names

http://msdn.microsoft.com/en-us/library/ie/81bads72%28v=vs.94%29.aspx

http://msdn.microsoft.com/en-us/library/vstudio/k6d7hcca%28v=vs.100%29.aspx

Hopefully someone around here can explain how they apply to GLUT controls.

EDIT: Oh wait here we go... It looks like your suspicions were correct about this allowing for simultaneous presses.

http://www.lighthouse3d.com/opengl/glut/index.php3?5

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

It's bitwise logic operators being used the same way that you can use the addition operator to say +=.

That is:

a |= b

is the same as:

a = a | b

Seeing the |= at the top there, I'd imagine that those variables are being given a value prior to what you posted. Since boolean values are either true or zero, these operations can be used as standard logic operators in the case that they're working on boolean values (in this case they are). You'd need to be careful with &= in cases where you're not using boolean inputs, since you can have two nonzero values that would be true for && but not for &.

The idea is that you can break up complex conditionals:


if(((a && b) || (c && d) || (x && y && z)) {
  //stuff
}

versus


bool condition = a && b;
condition |= c && d;
condition |= x && y && z;
if(condition) {
  //stuff
}

If this confuses you, do some research on bitwise logic and truth tables for and/or/xor. It should start to make sense once you have all the information.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

That's weird though... since the right part is only a true or false expression, it could be written as

BOOL mouseLClickButton = (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN);

so im assuming mouseLClickButton are integer, which is ratter poor design choice imo.

Or it might be a bool and wanted to get rid of warning or error messages, being too lazy to cast...

Edit: i just saw the &= afterward. Still, this code look weird somehow.

The |= and &= operations were already mentioned above. They are shorthand for bitwise operations.
The stuff on the right side of those operations is doing some logic operations to find a value of true (1) or false (0).

Going through each:

mouseLClickButton |= (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN);
mouseRClickButton |= (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN);
mouseMClickButton |= (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN);

In this case there are three variables. One for each mouse button.

The code first checks if the button matches the left, right or middle button. If it does not match then the result is false, or zero. The |= value is basically a=a|b. So a=a|false results in no change. If the button does match then the right side is evaluated. If that button is down then the state is true. So a=a|true means that the left hand side gets OR'ed with 1. So it sets the low bit to 1.

The end result is that if the indicated button is down the variable becomes either true (if it is a bool) or odd (if it is an integer). Otherwise the value is unchanged.


mouseLClickButton &= !(button == GLUT_LEFT_BUTTON && state == GLUT_UP);
mouseRClickButton &= !(button == GLUT_RIGHT_BUTTON && state == GLUT_UP);
mouseMClickButton &= !(button == GLUT_MIDDLE_BUTTON && state == GLUT_UP);

These three do similar work. First it matches the correct button. If that is true it checks for up. The result is then negated because of the !, so true becomes false and false becomes true. Putting it all together, if the left button is involved and the button is up, the result = false. If the left button is not involved, or if it is involved and the state is not up, then the result = true. &= is short for a=a&b.

The &= operation works like the logical OR for bools but is a little more tricky to explain for integers. So a=a&true means every bit except for the last becomes zero, and if the bit is already set then keep it. a=a&false means no matter what the bits used to be, clear them all. Basically force it to become 0.

The end result is that if the button is involved and if the button is up, the result becomes false (if it is a bool) or 0 (if it is an integer). Otherwise the value is unchanged (if it is a bool) or for integers, all bits other than the last one become zero and the last bit remains unchanged.



One key thing to notice is that using |= or &= means that if the button is not involved the value is unchanged. This is because they send an event for each mouse button. They get one event saying the left button is down, and a second event saying the middle button is down. When they get an event for the middle button it does not change the values for the right and left buttons. If they had used a plain assignment operator= they would potentially change the value if the button was involved. So pressing the middle button would mean the left button would be marked as being up, even if it really is down. Leaving the value unchanged is important.

I think i am understanding. mouseClickButton is in fact a bool and is actually never initialized until these conditional statements.

I am following what you say there frob but I am not understanding this a=a&b or a=a|b .

Is this (a=a&b) saying a is true if both a AND b are true?

and this (a=a|b) is saying a is true if a OR b is true?

J-GREEN

Greenpanoply

If those variables are never initialized, that sounds like a mistake.

In my opinion this is a bit easier to read:


  bool is_down = (state == GLUT_DOWN);
  switch (button) {
  case GLUT_LEFT_BUTTON:
    mouseLClickButton = is_down;
    break;
  case GLUT_RIGHT_BUTTON:
    mouseRClickButton = is_down;  
    break;
  case GLUT_MIDDLE_BUTTON:
    mouseMClickButton = is_down;
  }

I think i am understanding. mouseClickButton is in fact a bool and is actually never initialized until these conditional statements.

I am following what you say there frob but I am not understanding this a=a&b or a=a|b .

Is this (a=a&b) saying a is true if both a AND b are true?

and this (a=a|b) is saying a is true if a OR b is true?

Nope. Bitwise operations operate on bits.

For example, a |= b means effectively


for i from 0 to number of bits in a:
  if bit i of b is set, set the bit i of a
  otherwise leave the bit i of a unchanged

Similarly for the AND operation.

Stephen M. Webb
Professional Free Software Developer

Well, bools only have one bit, so for the case where a and b are both bools a = a & b does boil down to a is true if both a and b are true. Similarly, a = a | b does boil down to a is true if either a or b are true.

(To be pedantic, bools don't have a number of bits, but instead undergo integer promotion when placed in the context of bitwise operators, where true is promoted to 1 and false is promoted to 0.)

Alvaro, your example makes my eyes feel much better. And SiCrane thank you very much for your explanation.

Bregma is this diagram similar to your explanation of a|=b?


result = result | expression;

0101    (result)
1100    (expression)
----
1101    (output)
J-GREEN

Greenpanoply

This topic is closed to new replies.

Advertisement