1

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?

Matt
  • 1,747
  • 8
  • 33
  • 58
  • [This has been answered here already](http://stackoverflow.com/questions/3793650/convert-boolean-to-int-in-java) – asf107 Feb 02 '12 at 14:29
  • Think about renaming the question accordingly, because the title does not fit neither question nor answer. – mtsz Feb 02 '12 at 14:47

8 Answers8

3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • When I try this I get an error for `(!p)` saying `The operator ! is undefined for the argument type(s) int` the book does state that this may be a bit more complicated than first thought. – Matt Feb 02 '12 at 14:29
  • @Matt There is no unary equivalent to the `boolean` `!` (`~` flips all bits, not just the last one). the simplest way to achieve the result you are looking for is with `(1-p)` – Sergey Kalinichenko Feb 02 '12 at 14:35
2

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

} }

Nick
  • 21
  • 1
1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

Just use the ternary operator:

int logicalInt = boolVal? 1 : 0;

where "boolVal" is your boolean variable.

eternaln00b
  • 1,043
  • 9
  • 18
1

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
mtsz
  • 2,725
  • 7
  • 28
  • 41
0

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

}

Jaloopa
  • 722
  • 1
  • 6
  • 21
0

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");
Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
0

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.

Raphael
  • 1,701
  • 15
  • 26
  • When I try this I get an error for `(!p)` saying `The operator ! is undefined for the argument type(s) int` the book does state that this may be a bit more complicated than first thought. – Matt Feb 02 '12 at 14:30
  • As other people have said, you need to do 1-p instead of !p. – Raphael Mar 12 '12 at 15:18