Page 161 - Computer science 868 Class 12
P. 161

6.5.3 Logical Operators
                 The logical operators are the symbols or words that connect two or more relational expressions. They determine the
                 logic between the variables or values. Their output is the outcome of different conditions given and they always return
                 “true” or “false” depending on the relationship between the parameters of the expression.
                 There are three types of logical operators. They are as follows:

                             S. No.         Operator                Syntax                   Meaning

                               1      && (Logical AND)      expression 1 &&        Returns true only if both expression
                                                            expression 2           1 and expression 2 are true
                               2      || (Logical OR)       expression 1 ||        Returns true if either expression 1
                                                            expression 2           or expression 2 is true
                               3      ! (Logical NOT)       ! expression           Returns  true  if  the  expression  is
                                                                                   false and vice-versa

                 6.5.4 Bitwise Operators
                 Bitwise operator is a special operator which works on the bit level of the operands in an expression. The operands are
                 of integer literals (i.e., byte, int, short and long). It cannot work on real literals.

                 Let us see the different bitwise operators:

                                                   Operator                     Meaning
                                                      &               Bitwise AND
                                                      |               Bitwise OR
                                                      !               Bitwise NOT
                                                      ^               Bitwise XOR
                 1.  Bitwise AND (&): This operator works like a logical AND operator. It results in a single bit only.

                                          A                         B                         A&B
                                          0                         0                          0
                                          0                         1                          0
                                          1                         0                          0
                                          1                         1                          1
                 2.  Bitwise OR (|): This operator works like a logical OR operator. It results in a single bit only.

                                          A                         B                         A|B
                                          0                         0                          0
                                          0                         1                          1
                                          1                         0                          1
                                          1                         1                          1

                 3.  Bitwise XOR (∧): The output of this operator results in a low (0) for the inputs of the same value. And results in high
                   (1) for the inputs of the different values. It results in a single bit only.

                                          A                         B                         A∧B
                                          0                         0                          0
                                          0                         1                          1
                                          1                         0                          1
                                          1                         1                          0



                                                                                                                       159
                                                                                               Variables and Expressions   159
   156   157   158   159   160   161   162   163   164   165   166