d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Boolean Questions
Add Reply New Topic New Poll
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jan 21 2016 07:08pm
boolean b1, b2;
b1 = true;
b2 = false;

if(b1 & b2) System.out.println("this won't execute"); ///////////// (is asking if both b1 and b2 are true?)
if(!(b1 & b2)) System.out.println("!(b1 & b2) is true"); //////////////////(using not operator, asking NOT b1 & b2, meaning no b1/b2?)
if(b1 | b2) System.out.println("b1 | b2 is true"); /////////////////////(asking if b1 or b2 is true, only 1 needs true, correct?)
if(b1 ^ b2) System.out.println("b1 ^ b2 is true"); //////////////////////(not sure)



sorry using / cuz d2sjp doesn't allow space bar for spaces for some reason

This post was edited by ferf on Jan 21 2016 07:10pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 21 2016 07:13pm
use [CODE ]blocks for space.

as for your question, those are bitwise operators. don't use them for booleans. http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.2

if you want some advice, focus more on the programming you're likely to use and less on the obscure stuff you should never use.

This post was edited by carteblanche on Jan 21 2016 07:18pm
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jan 21 2016 07:23pm
EDIT: Problem solved

This post was edited by ferf on Jan 21 2016 07:52pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 21 2016 07:26pm
Quote (ferf @ Jan 21 2016 08:23pm)
bitwise operators wasn't the question, I'm asking what the conditionals mean


Notice how i put a question after each statement, i'd appreciate an answer for each question


did you read the link i sent you? i give up answering your posts. you're dead to me now.
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jan 21 2016 07:27pm
as i said, i wanna know what (b1) or (b2) does.

with:
boolean b1, b2;
b1 = true;
b2 = false;




EDIT: Nvm i figured it out

This post was edited by ferf on Jan 21 2016 07:52pm
Member
Posts: 10,415
Joined: Aug 24 2005
Gold: 24,533.60
Jan 22 2016 08:48am
Quote (ferf @ 22 Jan 2016 03:27)
as i said, i wanna know what (b1) or (b2) does.

with:
boolean b1, b2;
b1 = true;
b2 = false;




EDIT: Nvm i figured it out


from the link he gives you:
Quote
15.22.2. Boolean Logical Operators &, ^, and |
When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (ยง5.1.8) as necessary.
For &, the result value is true if both operand values are true; otherwise, the result is false.
For ^, the result value is true if the operand values are different; otherwise, the result is false.
For |, the result value is false if both operand values are false; otherwise, the result is true.
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jan 22 2016 01:20pm
Quote (EoN-mf @ Jan 22 2016 10:48am)
from the link he gives you:


Like i said, the question had nothing to do with operators...
Member
Posts: 13,578
Joined: Jul 27 2010
Gold: 2,285.00
Jan 23 2016 05:33pm
Quote (ferf @ Jan 22 2016 03:20pm)
Like i said, the question had nothing to do with operators...

Understanding the operators is actually a very important thing, so even if it's not what you explicitly asked, it's good for you to learn it anyway. Bitwise comparison isn't the same as boolean logic.

So, just to remove the operators entirely and focus on the question you were asking, let's use Python as the example. Its syntax is much easier to read.

Code
a = True
b = False

if a and b:
print This won't print. True and False is False

if not (a and b):
print This will print. True and False is False, but when you negate False you get True

if a or b:
print This will print. True or False is True

if a != b:
print Python doesn't have an Exclusive Or (XOR) operator that I know of, but XOR means "This is only true if both a and b have different values".
print This makes it true since True != False


This post was edited by bentherdonethat on Jan 23 2016 05:34pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll