Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Minor confusion about booleans  (Read 1512 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2011-04-30 19:53:07 »

This is a problem I should know the solution to..  persecutioncomplex I might have forgot it, because I know I've faces this before, where I've
had to hack it!

Alright. You have too booleans, and you want to execute a void if at least one of them returns true. How do you do that without using a couple of if-statements, or the longest if-statement ever? I see both using a couple of if's, and making an if so long that you need to scroll, as hacks and I would like to know the correct way to solve this, or go around it.  Cheesy

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-04-30 19:57:23 »

you want to execute a void
Can you explain that?

You have too booleans, and you want to execute a void if at least one of them returns true.

1  
2  
3  
4  
5  
6  
boolean a = ...;
boolean b = ...;
if( a || b )
{
    // void??
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline JL235

JGO Coder


Medals: 10



« Reply #2 - Posted 2011-04-30 20:23:31 »

I expect he means he wants to call a method with a void return type.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #3 - Posted 2011-04-30 20:52:35 »

I expect he means he wants to call a method with a void return type.

Exactly!

1  
2  
if (a || b) {
}


What if both are true?

Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #4 - Posted 2011-04-30 21:29:07 »

I expect he means he wants to call a method with a void return type.

Exactly!

1  
2  
if (a || b) {
}


What if both are true?

That if-case means "if either a or b is true, run this block". Since both of them are true, it will run the block. The only way to make it not run the block is if both a and b are false.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #5 - Posted 2011-04-30 22:26:57 »

I expect he means he wants to call a method with a void return type.

Exactly!

1  
2  
if (a || b) {
}


What if both are true?

That if-case means "if either a or b is true, run this block". Since both of them are true, it will run the block. The only way to make it not run the block is if both a and b are false.

Oh lord.. I've always been taught that those ment OR.. If a OR b is true.. not both..  Emo
I'll go back into my cave now. Thanks! Roll Eyes

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2011-04-30 22:29:10 »

for that we have XOR (exclusive OR)

1  
2  
3  
4  
if( a ^ b )
{

}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #7 - Posted 2011-04-30 22:56:23 »

for that we have XOR (exclusive OR)

1  
2  
3  
4  
if( a ^ b )
{

}


I always had problems using that correctly.. I imagine that operator does something different to ints Tongue

Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #8 - Posted 2011-04-30 23:09:19 »

for that we have XOR (exclusive OR)

1  
2  
3  
4  
if( a ^ b )
{

}


Maybe I'm just tired or something, but doesn't this code give the same result?

1  
2  
3  
if(a != b) {

}


Is there any advantage of using one over the other?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2011-05-01 00:32:53 »

In this case they are equal

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zlandorf

Junior Member


Medals: 1
Projects: 1



« Reply #10 - Posted 2011-05-01 01:42:15 »

I always had problems using that correctly.. I imagine that operator does something different to ints Tongue

the xor rules :

0 ^ 0 => 0
0 ^ 1 => 1
1 ^ 0 => 1
1 ^ 1 => 0

so, for instance 1101 ^ 110 = 1011


But, I must say that the only case I found it useful to use is to make a variable switch between 1 and 0.

I think in y86 (maybe other ASM languages ?) XOR is used to set 0 to a registry, because a ^a = 0, and it is quicker to do a XOR than to get the constant value in memory
Offline Scarzzurs
« Reply #11 - Posted 2011-05-01 09:12:52 »

Perhaps we are getting a bit off topic, but XOR can also be used for swapping values of two variables without the use of an extra variable. So in theory it has many uses...
This page also seems to have related information: http://stackoverflow.com/questions/160697/is-it-good-practice-to-use-the-xor-operator-in-java-for-boolean-checks

- Scarzzurs

My games and Projects:
BlastingPixels.com,
Old website
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #12 - Posted 2011-05-07 14:48:10 »

Perhaps we are getting a bit off topic, but XOR can also be used for swapping values of two variables without the use of an extra variable. So in theory it has many uses...
This page also seems to have related information: http://stackoverflow.com/questions/160697/is-it-good-practice-to-use-the-xor-operator-in-java-for-boolean-checks

- Scarzzurs

Thanks for the link. I have never used xor before (have heard of though) but that cleared up some question marks for me Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (75 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (35 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!