logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    cloudiiiday
    Nov 18, 2021

    11/17 Boolean Values and Operations

    in Introduction to Java

    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

    2 comments
    0
    cloudiiiday
    Nov 18, 2021

    Homework: https://docs.google.com/forms/d/e/1FAIpQLSduFmBGwV2YLNfGr6LPV_-Fii8DcL2QI8XRxdG9HeawtfkRaw/viewform?usp=sf_link

    0
    cloudiiiday
    Dec 02, 2021

    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

    0
    2 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners