Melissa Nikolic

Greenhorn

Posts: 12

posted 5 years ago

Hi,

Some feedback regarding my misunderstanding is appreciated.

1) The following is what the Java docs says about boolean logical negation ! operator:

"The type of the operand expression of the unary ! operator must be boolean, or a compile-time error occurs. The type of the unary logical complement expression is boolean. At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true."

15.15.6 Logical Complement Operator !

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html

2) Example (answers given from my book):

boolean b1, b2, b3 = false, b4 = false;

b1 = (4==2) & (1<4) // false

b2 = b1 | !(2.5>=8) //true

3) Question:

Why is b2 true? My understanding is that the second operand has the value of true, because the truth value for a Boolean OR is "true if either or both operands are true, otherwise false" and !(2.5>=8) is true (unless it's not true and this is where my problem is). Therefore, since one of the operands is true then the complement expression should be false (unless I don't understand the idea of complement expressions), and b2 = b1 | !(2.5>=8) should be false.

Thank you for your assistance.

Regards,

M

Some feedback regarding my misunderstanding is appreciated.

1) The following is what the Java docs says about boolean logical negation ! operator:

"The type of the operand expression of the unary ! operator must be boolean, or a compile-time error occurs. The type of the unary logical complement expression is boolean. At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true."

15.15.6 Logical Complement Operator !

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html

2) Example (answers given from my book):

boolean b1, b2, b3 = false, b4 = false;

b1 = (4==2) & (1<4) // false

b2 = b1 | !(2.5>=8) //true

3) Question:

Why is b2 true? My understanding is that the second operand has the value of true, because the truth value for a Boolean OR is "true if either or both operands are true, otherwise false" and !(2.5>=8) is true (unless it's not true and this is where my problem is). Therefore, since one of the operands is true then the complement expression should be false (unless I don't understand the idea of complement expressions), and b2 = b1 | !(2.5>=8) should be false.

Thank you for your assistance.

Regards,

M

Greg Brannon

Bartender

Posts: 563

posted 5 years ago

Booleans can get our brains twisted the more we think about them.

2.5 >= 8 is false. 2.5 is clearly NOT greater than or equal to 8.

!( false ) = true. obvious?

b2 = b1 || true

b1 = false. a given

b2 = false || true. If either this OR that is true, the result is true.

b2 = true.

2.5 >= 8 is false. 2.5 is clearly NOT greater than or equal to 8.

!( false ) = true. obvious?

b2 = b1 || true

b1 = false. a given

b2 = false || true. If either this OR that is true, the result is true.

b2 = true.

Always learning Java, currently using Eclipse on Fedora.

Linux user#: 501795

Campbell Ritchie

Marshal

Posts: 56534

172

Campbell Ritchie

Marshal

Posts: 56534

172

posted 5 years ago

Personally, I like just calling it "not", especially for a boolean. 'Compliment' is somewhat vague, and in the context of '~', it's actually a 1s-compliment op.

Winston

Campbell Ritchie wrote:I see they are calling it complement. Still probably in accurate. Negation is better.

Personally, I like just calling it "not", especially for a boolean. 'Compliment' is somewhat vague, and in the context of '~', it's actually a 1s-compliment op.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Melissa Nikolic

Greenhorn

Posts: 12

posted 5 years ago

Thanks for your reply. I do get your explanation but I was confused by the documentation which said, " At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true". So, I thought the complement of the true value would be false (following my logic?).

I understand the example (re: why b2 is true) but what is the documentation referring to from the above quote?

Thanks again.

M

Greg Brannon wrote:Booleans can get our brains twisted the more we think about them.

2.5 >= 8 is false. 2.5 is clearly NOT greater than or equal to 8.

!( false ) = true. obvious?

b2 = b1 || true

b1 = false. a given

b2 = false || true. If either this OR that is true, the result is true.

b2 = true.

Thanks for your reply. I do get your explanation but I was confused by the documentation which said, " At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true". So, I thought the complement of the true value would be false (following my logic?).

I understand the example (re: why b2 is true) but what is the documentation referring to from the above quote?

Thanks again.

M

Melissa Nikolic

Greenhorn

Posts: 12

posted 5 years ago

In reference to the documentation, "The type of the operand expression of the unary ! operator must be boolean, or a compile-time error occurs. The type of the unary logical complement expression is boolean. At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true."

In this context "complement" means the "boolean logical operator negation" or '!' or "not"? So, what does it mean that "the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true". I was thinking that the complement of true is false and vice versa (hence my original confusion with the stated example).

Thank you,

M

Winston Gutkowski wrote:Campbell Ritchie wrote:I see they are calling it complement. Still probably in accurate. Negation is better.

Personally, I like just calling it "not", especially for a boolean. 'Compliment' is somewhat vague, and in the context of '~', it's actually a 1s-compliment op.

Winston

In reference to the documentation, "The type of the operand expression of the unary ! operator must be boolean, or a compile-time error occurs. The type of the unary logical complement expression is boolean. At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true."

In this context "complement" means the "boolean logical operator negation" or '!' or "not"? So, what does it mean that "the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true". I was thinking that the complement of true is false and vice versa (hence my original confusion with the stated example).

Thank you,

M

posted 5 years ago

It is. The logical or boolean complement of true is false.

It's saying that the value of the expression !X is the complement (the opposite) of the value of the expression X. That is, the expression which consists of operator ! and its operand X has the opposite value to what X has.

This is

Thus, the value of the right hand operand of the | operator is true, because the ! gives us the complement of (2.5 >= 8), i.e., the complement of false, i.e. true.

Melissa Nikolic wrote:

Thanks for your reply. I do get your explanation but I was confused by the documentation which said, " At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true". So, I thought the complement of the true value would be false (following my logic?).

It is. The logical or boolean complement of true is false.

I understand the example (re: why b2 is true) but what is the documentation referring to from the above quote?

It's saying that the value of the expression !X is the complement (the opposite) of the value of the expression X. That is, the expression which consists of operator ! and its operand X has the opposite value to what X has.

This is

**b2 = X | Y**where Y is

**!Z**and Z is

**(2.5 >= 8)**.

Thus, the value of the right hand operand of the | operator is true, because the ! gives us the complement of (2.5 >= 8), i.e., the complement of false, i.e. true.

posted 5 years ago

It means that if the value of the operand expression X is true, then the operator ! makes the value of the expression !X into false, and vice versa.

Which is exactly correct when talking about booleans, which is what we are doing here.

I'm totally lost as to what you're confused about. The only think I can think of is that you took the documentation to mean that the expression

Melissa Nikolic wrote:

In this context "complement" means the "boolean logical operator negation" or '!' or "not"? So, what does it mean that "the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true".

It means that if the value of the operand expression X is true, then the operator ! makes the value of the expression !X into false, and vice versa.

I was thinking that the complement of true is false and vice versa

Which is exactly correct when talking about booleans, which is what we are doing here.

I'm totally lost as to what you're confused about. The only think I can think of is that you took the documentation to mean that the expression

**X | !Y**would have the opposite value to the expression

**X | Y**because there happens to be a ! somewhere in the overall expression. That's not what the documentation is saying. The ! only negates its one operand--in this case, the Y. And

**X OR NOT Y**is more definitely not the same as

**NOT(X OR Y)**.