Page 477 - ComputerScience_Class_11
P. 477

The following table shows the bitwise operators in Python along with their description:

                    Operator                                           Description
                   & (Bitwise AND)  Returns 1 in each bit position if both corresponding bits are 1.
                   | (Bitwise OR)  Returns 1 in each bit position if at least one corresponding bit is 1.
                   ^ (Bitwise XOR)  Returns 1 in each bit position if the corresponding bits are different.
                   ~ (Bitwise NOT)  Returns the inverted value of all bits (0 becomes 1 and 1 becomes 0).
                   << (Left Shift)  Returns the value after shifting the bits to the left by a specified number of positions.
                   >> (Right Shift)  Returns the value after shifting the bits to the right by a specified number of positions.

                 Program 10: To demonstrate the use of Bitwise operators.
                     Program 10.py

                  File  Edit  Format   Run   Options   Window     Help

                  # Assigning values to variables
                  a = 5    # Binary: 0101
                  b = 3    # Binary: 0011

                  # Bitwise AND
                  print("a & b:", a & b)

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

                  # Bitwise XOR
                  print("a ^ b:", a ^ b)

                  # Bitwise NOT
                  print("~a:", ~a)

                  # Left Shift
                  print("a << 1:", a << 1)

                  # Right Shift
                  print("a >> 1:", a >> 1)




                     Output

                  a & b: 1
                  a | b: 7
                  a ^ b: 6
                  ~a: -6
                  a << 1: 10
                  a >> 1: 2













                                                                                           Operators and Expressions  475
   472   473   474   475   476   477   478   479   480   481   482