0% found this document useful (0 votes)
5 views2 pages

Python Bitwise Operators Exercises

Uploaded by

Het Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Python Bitwise Operators Exercises

Uploaded by

Het Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE 5

Logical operators: Compare bits of the given object and always return a Boolean result.
Bitwise operators: Perform operations on individual bits, and the result is also always a
bit.

Python Programming Questions (Bitwise Operators)

Q1. Bitwise AND


Write a Python program to take two integers as input (a and b) and calculate their bitwise
AND (a & b). Display the input values and the result.

Example:

Input:
Enter first number: 15
Enter second number: 9

Output:
Bitwise AND of 15 and 9 is: 9

Q2. Bitwise OR
Write a Python program to take two integers as input (a and b) and calculate their bitwise
OR (a | b). Display the input values and the result.

Example:

Input:
Enter first number: 12
Enter second number: 5

Output:
Bitwise OR of 12 and 5 is: 13

Q3. Bitwise XOR


Write a Python program to take two integers as input (a and b) and calculate their bitwise
XOR (a ^ b). Display the input values and the result.

Example:

Input:
Enter first number: 10
Enter second number: 7

Output:
Bitwise XOR of 10 and 7 is: 13

Q4. Bitwise NOT (Complement)


Write a Python program to take an integer a as input and calculate its bitwise complement
(~a). Display the input value and the result.

Example:

Input:
Enter a number: 20

Output:
Bitwise NOT of 20 is: -21

Q5. Left Shift and Right Shift


Write a Python program to demonstrate left shift (<<) and right shift (>>) operations.
Take an integer as input and shift it by a specified number of bits. Display the original
value and the results of both shifts.

Example:

Input:
Enter a number: 50
Enter the number of bits to shift: 2

Output:
Left shift by 2: 200
Right shift by 2: 12

Q6. Combined Bitwise Expression


Write a Python program to take two integers a and b as input and compute the following
expression:

result = (a & b) | (~b ^ a)

Display the result.

You might also like