Search code examples
javaintnot-operatorboolean-algebra

using ! on int in JAVA


I was working on a program in java where I was creating a truth table of a full subtractor using a 2 dimensional array. While doing so, I wanted to compute the borrow out with the following formula: B(out) = !(X).Y + (!(X ^ Y))B(in)

So, I wrote it like:

table[i][4] = ((!(table[i][2]))&table[i][1])+((!(table[i][2]^table[i][1]))*table[i][0]);

(here: table[i][4] = cell to store borrow out; 
       table[i][2] = cell storing X; 
       table[i][1] = cell storing Y; 
       table[i][0] = cell storing borrow in B(in) )

Whereas XOR(^) and AND(&) and or(|) worked fine, I got the following error for NOT(!):

operator ! cannot be applied to int 

How can I correct this? Is there any other way to write this formula as a JAVA code? Please help.


Solution

  • The bitwise complement operator that can be applied to integer values is the ~, not !