Is there a way to change boolean into integers?

Like... making every "TRUE" = 1 and "FALSE" = 0

If there is a way, how will you do it?
I tried using this 'cast' thing, but it's confusing :'(


- - - - -
I'm suppose to make a table, which I did..

class LogicalOpTable {
public static void main(String args[]) {

boolean p, q;

System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

p = true; q = true;
System.out.print(p + "\t" + q + "\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + ( !p));

p = true; q = false;
System.out.print(p + "\t" + q + "\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + ( !p));

p = false; q = true;
System.out.print(p + "\t" + q + "\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + ( !p));

p = false; q = false
System.out.print(p + "\t" + q + "\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + ( !p));
}
}


(Sorry, I don't know how to make it into that box thingy, so I just made it green color)

The next step tells me to:

"On your own, try modifying the program so that it uses and displays 1's and 0's, rather than true and false. This may involve a bit more effort than you might at first think!"

Yeah, ignore the "On your own" part :)

__________

Thanks guys, I will get the hang of this soon enough... I hope... :S

Just use a tertiary expresssion:
int value = booleanVar ? 1 : 0;

Oh, I haven't learned that yet, thanks.

Where would I go about adding this?

I think that is best up to you to decide for the exercise.

I mean, not where do I add it but..

it would look like this right?

int value = p, q? 1 : 0;

A tertiary expression is evaluated like so:
(logical expression) ? (value if true) : (value if false)
So given your values of p and q, you can use them each with an expression such as

p ? 1 : 0
q ? 1 : 0

Tertiary expressions can be used most anywhere that a variable could be used, though in many cases you will need to surround them with parens to force evaluation before the rest of the expression. Example:

p = false; q = false
  System.out.print((p ? 1 : 0) + "\t" + (q ? 1 : 0) + "\t");

I am not necessarily saying that is the best way to accomplish changing your program to use 1 and 0 instead of true and false, but tertiary expressions are a way to return 2 different values based upon a logical expression.

int value = p, q? 1 : 0;

This statement would not quite work (as the compiler will tell you rather quickly), instead something like this would be needed:

int intP = p ? 1 : 0;
int intQ = q ? 1: 0;

(I just arbitrarily used the variable intP for the integer value of p, so don't be confused by the double occurrence of int :) )

int intP = p ? 1 : 0;
int intQ = q ? 1: 0;

Where would I add the intP/ intQ?

do I need to change

System.out.print(p + "\t" + q + "\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + ( !p));

If so, what would I need to change it to?
Do I change the p into intP and q into intQ?

Is there a different way to do this? Since I haven't learned about this expression, I doubt the book is trying to make me do it this way.. it also is confusing me.. or maybe I'm a tad bit slow... :|

Ternary expressions are actually just an elegant way of writing simple if...then...else expressions, so you could of course also write

if (p==true)
   intP = 1;
if (p ==false)
  intP = 0;

I don't know, if you have already learned about manipulating bits using and, or and xor, but in fact "true" and "false" are internally represented as 1 and 0, so you could just change "p" to "intP" and "q" to "intQ". Give it a try, it won't hurt :-)

With kind regards

Johannes Riecken

Ternary expressions are actually just an elegant way of writing simple if...then...else expressions, so you could of course also write

if (p==true)
   intP = 1;
if (p ==false)
  intP = 0;

I don't know, if you have already learned about manipulating bits using and, or and xor, but in fact "true" and "false" are internally represented as 1 and 0, so you could just change "p" to "intP" and "q" to "intQ". Give it a try, it won't hurt :-)

With kind regards

Johannes Riecken

Oh crap! I forgot about the 'if' thing!
Thanks thanks!..

That would mean I would change

System.out.print(p + "\t" + q + "\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + ( !p));

the 'p's into intP and 'q's into intQ in that code?
>>

I don't know, if you have already learned about manipulating bits using and, or and xor

I'm having troubles understanding what the xor is..

exclusive OR? -- what is that suppose to mean? :[

That would mean I would change

System.out.print(p + "\t" + q + "\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + ( !p));

the 'p's into intP and 'q's into intQ in that code?

That's right.

I'm having troubles understanding what the xor is..

exclusive OR? -- what is that suppose to mean? :[

exclusive OR only returns "true" if one of the two conditions is true, not both (that's the exclusive part in contrast to the normal OR).
So we have
true XOR true = false
true XOR false = true
false XOR true = true
false XOR false = false

This question has already been answered. Start a new discussion instead.