Sign in to follow this  
Followers 0
pressgreen

funky looking conditional statement i think

18 posts in this topic

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.

Edited by greenzone
0

Share this post


Link to post
Share on other sites

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

2

Share this post


Link to post
Share on other sites

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.

Edited by Khatharr
3

Share this post


Link to post
Share on other sites

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.

Edited by Vortez
0

Share this post


Link to post
Share on other sites
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.
3

Share this post


Link to post
Share on other sites

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?

Edited by greenzone
0

Share this post


Link to post
Share on other sites

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;
  }

Edited by Álvaro
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
Well, bools only have one bit, so for the case where [tt]a[/tt] and [tt]b[/tt] are both bools [tt]a = a & b[/tt] does boil down to [tt]a[/tt] is true if both [tt]a[/tt] and [tt]b[/tt] are true. Similarly, [tt]a = a | b[/tt] does boil down to [tt]a[/tt] is true if either [tt]a[/tt] or [tt]b[/tt] 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.)
3

Share this post


Link to post
Share on other sites

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)
Edited by greenzone
0

Share this post


Link to post
Share on other sites


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

Indeed it is. 

1

Share this post


Link to post
Share on other sites

(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.)

(except when they don't. like when some asshole vendor has decided to store flags in the other 31 bits of their boolean)

(I will be scarred for the rest of my life)
0

Share this post


Link to post
Share on other sites

 

(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.)

(except when they don't. like when some asshole vendor has decided to store flags in the other 31 bits of their boolean)

(I will be scarred for the rest of my life)

 

 

That's why I don't like it when people can't just use the integrated bool type. It seems like every lib that comes along these days has to have its own proprietary bool.

0

Share this post


Link to post
Share on other sites

I became a lot more sympathetic to the idea of library defined boolean types when I first ran across a platform with an 8 byte bool. And, no, this wasn't a platform where everything was 64 bits. sizeof(bool) was 8. gcc on Alpha IIRC. 

0

Share this post


Link to post
Share on other sites

That's why I don't like it when people can't just use the integrated bool type. It seems like every lib that comes along these days has to have its own proprietary bool.

That *was* the integrated bool type.

See, C++ doesn't really enforce many rules. And if you happen to know that on your platform, the compiler allocates 4 bytes for a bool, then you can *(int)&booleanValue, and proceed to manipulate the bits...

0

Share this post


Link to post
Share on other sites

That's why I don't like it when people can't just use the integrated bool type. It seems like every lib that comes along these days has to have its own proprietary bool.

That *was* the integrated bool type.

See, C++ doesn't really enforce many rules. And if you happen to know that on your platform, the compiler allocates 4 bytes for a bool, then you can *(int)&booleanValue, and proceed to manipulate the bits...


Burn it with fire.
0

Share this post


Link to post
Share on other sites


See, C++ doesn't really enforce many rules. And if you happen to know that on your platform, the compiler allocates 4 bytes for a bool, then you can *(int)&booleanValue, and proceed to manipulate the bits...

 

This is actually very dangerous when doing language interop, even when the boolean types have the same size. C/C++ would seem to assume that 0 is false and anything else is true, but it is by no means certain that this is the same for all languages. For instance some other languages consider even = false and odd = true (i.e. they look only at the least significant bit, which imho makes more sense, but whatever). In these situations if you hack your "boolean" variable to equal, say, 42 in C++, this will evaluate to true in C++ but false in that other language. And it's not fun to debug.

 

It happened to me once. I was wondering why the boolean returned by a call to a C++ library I used from Pascal years ago didn't evaluate to what it should have. Turned out their idea of a boolean was not true/false but more like true/false + handle to some internal structure (because apparently, returning that handle by reference wasn't good enough and they needed to squeeze a copy in the return value "for my convenience"). /sigh

 

But this all goes with the rest.. people trying to be clever by finding loopholes in the standard and ending up shooting themselves (or, preferably, their users) in the foot.

0

Share this post


Link to post
Share on other sites


This is actually very dangerous when doing language interop, even when the boolean types have the same size. C/C++ would seem to assume that 0 is false and anything else is true, but it is by no means certain that this is the same for all languages.

Bingo.

 

Proprietary embedded chip, with 4-byte native alignment, and a CMP instruction that only looked at the least-significant bit.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0