Logical gates with integer values
First part

You want to iterate through all possible combinations of for a pair of booleans, so you can make the code reflect that explicitly and make it simpler:

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

    for (boolean p : new boolean[] {true, false}) {
        for (boolean q : new boolean[] {true, false}) {
            System.out.print(p + "\t" + q + "\t" + (p&amp;&amp;q) + "\t");
            System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
        }
    }
    System.out.println();


Second part

You have something suspicious in your if statements. As you don't use braces, this code smells of a copy&amp;paste bug:

    if(a==0 &amp;&amp; b == 0 )
        not = 1;
        System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));


If the condition is met, it will execute the line not = 1;. The second line will be executed independently of the condition. I strongly suggest you use braces even for 1-line blocks, like this:

    if (a == 0 &amp;&amp; b == 0) {
        not = 1;
        System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
    }


This way you can avoid this kind of bugs.

Now, applying the same reasoning as with the previous method, you can rewrite it as:

    System.out.println("A\tB\tAND\tOR\tXOR\tNOT");
    for (int a : new int[] {0, 1}) {
        for (int b : new int[] {0, 1} ) {
            System.out.println(a + "\t" + b + "\t" + (a &amp; b) + "\t" + (a | b) + "\t" + (a ^ b) + "\t" + ~a);
        }
    }


BUT What do you want to accomplish with the NOT operation? Do you mean the Bitwise Complement? I used that, but maybe you want a function that returns 0 when it's 1, and 1 when it's 0. In that case, you need to replace the ~a with something like (a == 0) ? 1 : 0.

So the whole code could be reduced to just:

public static void printTable() {
    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
    for (boolean p : new boolean[]{true, false}) {
        for (boolean q : new boolean[]{true, false}) {
            System.out.print(p + "\t" + q + "\t" + (p&amp;&amp;q) + "\t");
            System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
          }
    }
    System.out.println();
    System.out.println("A\tB\tAND\tOR\tXOR\tNOT");
    for (int a : new int[]{ 0, 1}) {
        for (int b : new int[]{0, 1} ) {
            System.out.println(a + "\t" + b + "\t" + (a &amp; b) + "\t" + (a | b) + "\t" + (a ^ b) + "\t" + ~a);
        }
    }
}