Matt
Matt

Reputation: 1757

Casting Boolean To Int

I am going through a tutorial where so far it gives you the code below:

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

The task is to modify the program so that it uses 1's and 0's instead of true and false.

Im not sure if this is ment to be done by Casting Incompatible Types but I think that is the way to go as that is the section before it.

Can anyone give some advice and explanation as to why it works?

Upvotes: 1

Views: 4429

Answers (8)

Nick
Nick

Reputation: 21

This was my solution //Logic table using 1 and 0

class LogicOpTable {

public static void main(String args[]) {

   int p, q;

   System.out.println("P\tQ\t"+
             "AND\tOR\tXOR\tNOT");
   for (p = 1; p >= 0; p--) {
    for (q = 1; q >= 0; q--) {

   System.out.println(p + "\t" + q + "\t" +
             (p & q) + "\t" + (p | q) + 
                     "\t" + (p ^ q) + "\t" + (1 - p));
     }
  }               

} }

Upvotes: 2

jestem_wojtek
jestem_wojtek

Reputation: 81

You cannot cast int to boolean in java. Think of using

boolean x = p & q;
boolean y = p | q;

(x ? 1 : 0)

For example

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

May become:

System.out.print(x + "\t" + y + "\t");

Upvotes: 0

mtsz
mtsz

Reputation: 2855

You can use integers together with bitwise operators:

int p, q;

System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
p = 1;
q = 1;

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

p = 1;
q = 0;
System.out.print(p + "\t" + q + "\t");
System.out.print((p & q) + "\t" + (p | q) + "\t");
System.out.println((p ^ q) + "\t" + (1-p));

p = 0;
q = 1;
System.out.print(p + "\t" + q + "\t");
System.out.print((p & q) + "\t" + (p | q) + "\t");
System.out.println((p ^ q) + "\t" + (1-p));

p = 0;
q = 0;
System.out.print(p + "\t" + q + "\t");
System.out.print((p & q) + "\t" + (p | q) + "\t");
System.out.println((p ^ q) + "\t" + (1-p));

Returns:

P   Q   AND OR  XOR NOT
1   1   1   1   0   0
1   0   0   1   1   0
0   1   0   1   1   1
0   0   0   0   0   1

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

This is not what the tutorial asks you to do. I think they want you to literally replace boolean with int, true with 1, and false with 0, like this:

int p, q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
p = 1; q = 1;
System.out.print(p + "\t" + q + "\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (1-p)); // EDIT: was !p

This will lead you to understanding of bitwise operations on integers 0 and 1.

Upvotes: 3

eternaln00b
eternaln00b

Reputation: 1053

Just use the ternary operator:

int logicalInt = boolVal? 1 : 0;

where "boolVal" is your boolean variable.

Upvotes: 1

Raphael
Raphael

Reputation: 1721

It looks like you just need to declare your variables as ints and assign 0 and 1 to p and q, and make sure you're using java's bitwise operators in all cases (at first glance it looks like you are). More info on bitwise operation from wikipedia.

Upvotes: 0

Jaloopa
Jaloopa

Reputation: 750

You can't cast directly from a boolean to an int in Java. I would add a method along the lines of

public int getBoolValue(boolean b) {
 return b ? 1 : 0

}

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691765

You can't cast a boolean to an int. These are completely different types.

But you can write a utility method booleanToInt(boolean b) which transforms a boolean into an int.

Upvotes: 1

Related Questions