Often we have to compare different expressions or variables. Boolean variables only have 2 values, true and false or 1 and 0 (1 corresponds to true, 0 corresponds to false).
Bitwise operations:
AND, * (logic product)
0 * 0 = 0
1 * 0 = 0
0 * 1 = 0
1 * 1 = 1
OR, + (logic sum)
0 + 0 = 0
1 + 0 = 1
0 + 1 = 1
1 + 1 = 1
NOT, ! (logic negation)
!0 = 1
!1 = 0
XOR, ⊕ or ^
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
XNOR, ⊙ (opposite of ⊕)
0 ⊙ 0 = 1
1 ⊙ 0 = 0
0 ⊙ 1 = 0
1 ⊙ 1 = 1
Order of Precedence: () > ! > * > ⊕ = ⊙ > +
Boolean Expressions:
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
Logical Operators:
! Logical NOT
&& Logical AND
a&&b is true only if both a and b are true
|| Logical OR
a||b is true if a or b or both are true
Comparison:
char types are compared by ACSII values
Strings are compared by ACSII values of each character from left to right
When paring two double or float values, we often compare the difference with a small enough value because of round off errors
A boolean variable x == true is the same as x
Selected Operators and their Precedence:
[] Array element access
++, --, ! Increment, decrement, Boolean not
*, /, % Multiplication, division, remainder
+, - Addition, subtraction
<, <=, >, >= Comparisons
==, != Equal, not equal
&& Boolean and
|| Boolean or
= Assignment
If x is an int variable with value 5 and y is an int variable with value 5, what is the value of the expression !(x > y)?
Answer: True
Homework: https://docs.google.com/forms/d/e/1FAIpQLSduFmBGwV2YLNfGr6LPV_-Fii8DcL2QI8XRxdG9HeawtfkRaw/viewform?usp=sf_link