Page 62 - Touhpad Ai
P. 62

Bitwise Operators
              Bitwise operators perform bit-level operations on operands. They are used to manipulate individual bits of integers.


                  Operator            Name                                      Description

                     &       Bitwise AND              Returns bit 1, if both bits are 1; otherwise returns bit 0.
                      |      Bitwise OR               Returns bit 1, if any of the bits is 1; otherwise returns bit 0.

                     ^       Bitwise XOR              Returns bit 1, if any of the bits is 1 but not both; otherwise returns bit 0.
                     ~       Bitwise NOT (complement) Inverts individual bits.

                                                      It moves all bits in a binary number to the left by a certain number of
                     <<      Bitwise left shift
                                                      positions.
                                                      It moves all bits in a binary number to the right by a certain number of
                     >>      Bitwise right shift
                                                      positions.

                 Program 9: To demonstrate the use of bitwise operators

                 # Uses of Bitwise Operators

                 # Bitwise AND
                 a = 10  # Binary: 1010
                 b = 6   # Binary: 0110
                 result_and = a & b
                 print("Bitwise AND:", result_and)

                 # Bitwise OR
                 result_or = a | b
                 print("Bitwise OR:", result_or)

                 # Bitwise XOR
                 result_xor = a ^ b
                 print("Bitwise XOR:", result_xor)
                 # Bitwise NOT
                 result_not = ~a

                 print("Bitwise NOT:", result_not)
                 # Bitwise Left Shift
                 result_left_shift = a << 2
                 print("Bitwise Left Shift:", result_left_shift)

                 # Bitwise Right Shift
                 result_right_shift = a >> 2
                 print("Bitwise Right Shift:", result_right_shift)

                 Output:
                 Bitwise AND: 2

                 Bitwise OR: 14
                 Bitwise XOR: 12


                 60     Touchpad Artificial Intelligence - XI
   57   58   59   60   61   62   63   64   65   66   67