Jump to content

  • Log In with Google      Sign In   
  • Create Account

funky looking conditional statement i think


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
18 replies to this topic

#1 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 27 June 2013 - 07:55 PM

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, 27 June 2013 - 07:59 PM.

J-GREEN

Greenpanoply

Sponsor:

#2 marcClintDion   Members   -  Reputation: 431

Like
2Likes
Like

Posted 27 June 2013 - 08:14 PM

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, because you know that the testing of your faith produces perseverance. Let perseverance finish its work so that you may be mature and complete, not lacking anything.


#3 Khatharr   Crossbones+   -  Reputation: 3039

Like
3Likes
Like

Posted 27 June 2013 - 09:33 PM

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, 27 June 2013 - 09:42 PM.

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.

#4 Vortez   Crossbones+   -  Reputation: 2704

Like
0Likes
Like

Posted 27 June 2013 - 09:41 PM

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, 27 June 2013 - 09:46 PM.


#5 frob   Moderators   -  Reputation: 22719

Like
3Likes
Like

Posted 27 June 2013 - 11:09 PM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#6 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 28 June 2013 - 06:45 AM

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, 28 June 2013 - 06:50 AM.

J-GREEN

Greenpanoply

#7 Álvaro   Crossbones+   -  Reputation: 13908

Like
1Likes
Like

Posted 28 June 2013 - 06:57 AM

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, 28 June 2013 - 07:09 AM.


#8 Bregma   Crossbones+   -  Reputation: 5439

Like
0Likes
Like

Posted 28 June 2013 - 08:00 AM

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

#9 SiCrane   Moderators   -  Reputation: 9669

Like
3Likes
Like

Posted 28 June 2013 - 08:19 AM

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

#10 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 28 June 2013 - 10:39 AM

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, 28 June 2013 - 10:49 AM.

J-GREEN

Greenpanoply

#11 Bregma   Crossbones+   -  Reputation: 5439

Like
1Likes
Like

Posted 28 June 2013 - 11:35 AM


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

Indeed it is. 


Stephen M. Webb
Professional Free Software Developer

#12 superman3275   Crossbones+   -  Reputation: 2061

Like
0Likes
Like

Posted 28 June 2013 - 12:10 PM

Bitwise operators and Bit Masks.


I'm a game programmer and computer science ninja ph34r.png!

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

 

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here smile.png!


#13 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 28 June 2013 - 03:14 PM

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

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#14 Khatharr   Crossbones+   -  Reputation: 3039

Like
0Likes
Like

Posted 28 June 2013 - 05:14 PM

 

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


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.

#15 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 28 June 2013 - 05:47 PM

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. 



#16 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 28 June 2013 - 06:32 PM

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


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#17 Khatharr   Crossbones+   -  Reputation: 3039

Like
0Likes
Like

Posted 28 June 2013 - 07:00 PM

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

#18 Bacterius   Crossbones+   -  Reputation: 9281

Like
0Likes
Like

Posted 28 June 2013 - 07:13 PM


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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#19 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 28 June 2013 - 08:41 PM


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.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS