7

I am trying to simplify the following using DeMorgan's Law: ! (x!=0 || y !=0)

Does x!=0 simplify to x>0? Or am I wrong in the following:

 !(x>0 || y>0)
 !(x>0) && !(y>0)
 ((x<=0) && (y<=0))

Thanks.

user3003605
  • 385
  • 4
  • 6
  • 13
  • If it's a positive number, then yes, (x!=0) <-> (x>0). In programming you will usually handle only positive numbers. So it's usually the same depending on the problematic, although when you don't know what X is, it is **wrong**. – Fabinout Nov 18 '13 at 09:01
  • ! (x!=0 || y !=0), your original statement, is about as simple as it gets I suppose. Though, to properly simplify Boolean algebra you have to convert all the terms to variables, true, and false. Taking 0 as false yields !(x||y) which could be converted into a conjunction !x && !y but, since 0 actually represents a number in your statement you would have to consider != 0 somewhere else for both terms and use the Boolean results in your logical statement. For sure, when dealing with a boolean set of numbers (1 and 0) any x!=0 is x>0, though for number sets including negatives it is not true. – Kastor Nov 18 '13 at 20:37

6 Answers6

7

Does x!=0 simplify to x>0?

No that's not true. Because integers are signed.


How to simplify : !(x!=0 || y !=0) ?

Consider this rules :

  1. enter image description here (second De Morgan's laws )

  2. enter image description here

By 1., it implies

!(x!=0 || y !=0) <=> (!(x!=0)) && (!(y != 0))

By 2., it implies

(!(x!=0)) && (!(y != 0)) <=> (x == 0) && (y == 0)


To test you can write the following loop :
for(int x = -5; x < 5; x++){
     for(int y = -5; y < 5; y++){
         if(!(x!=0 || y !=0))
            System.out.println("True : ("+x+","+y+")");
    }
}
Community
  • 1
  • 1
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
3

DeMorgans Law states the following:

!(A & B) = !A | !B    (I)
!(A | B) = !A & !B    (II)

In your case (II) applies: !(x!=0 || y!=0) => !(x!=0) && !(y!=0) => (x==0) && (y==0)

PS: Your question: "Does x!=0 simplify to x>0?" can be answered with "no" unless x can not take negative values (for example if the type of x is unsigned).

brimborium
  • 9,362
  • 9
  • 48
  • 76
1

Does x!=0 simplify to x>0? Or am I wrong in the following:

x != 0  // reads x does not equal 0; any number BUT 0

x > 0 // reads x is greater than 0; only numbers greater than 0

Do these two look the same, when you write it out like this?

Combined

(x != 0 && x > 0) // any number above 0
(x != 0 || x > 0) // any number BUT 0
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

In java integers are always signed so it is not necessarily true that x!=0 is the same as x>0

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
0

The conversion of the first two comparisons according to De' Morgan's law is the following.

 !(x>0 || y>0)        --->      x <= 0 && y <= 0

 !(x>0) && !(y>0)     --->      !(x <=0 || y <=0)   
Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
0

When I teach how to write Java do-while loops, I explain how to write the condition which terminates the loop.

For example, if I want to ask the user to enter a value which must be 0, 1, 2, or 3, I want the while condition to continue if the input value is not (value >= 0 and value <= 3).

This translates into while (!(value >= 0) or !(value <= 3)).

But !(value >= 0) means (value < 0), and !(value <= 3) means (value > 3), so the while loop is written as while (value < 0 || value > 3).

Hope this helps.