Truth Tables and De Morgan's Rules
7. Precedence of NOT
Answer:Yes. Sometimes boolean variables like
rejectare used to record a decision that might be used in
several places in the program.
Precedence of NOT
The NOT operator has high precedence. It is done before arithmetic and relational operators unless you use parentheses. Examine the following:
!speed > 2000 && memory > 512
illegal: can't use ! on an arithmetic variable
! has high precedence, the above says to apply it to
speed. This which won't work, because
speed is an integer and
! applies only to boolean
When parentheses are used
!(speed > 2000 && memory > 512)
! is the last operation done and is applied to the boolean value of the expression inside parentheses.
Expressions that involve a NOT operator are often hard to read. A confusing expression can be sometimes be rewritten to eliminate the NOT.
Look at the original fragment:
if ( !( speed > 2000 && memory > 512 ) ) System.out.println("Reject this computer"); else System.out.println("Acceptable computer");
Does the following do the same thing?
if ( speed <= 2000 || memory <= 512 ) System.out.println("Reject this computer"); else System.out.println("Acceptable computer");
Use some example values for
memoryand figure out what each fragment does.