Unit 1 Lesson 10
Unit 1 Lesson 10
a)20
b)100
c)5
d None of these
/= Divides left operand with the right and c /= a, equivalent to c = c / a
assign the result to left operand
**= Performs exponential calculation on left and right operand c **= a, equivalent to c = c ** a
and assign the result to left operand
//= Performs floor division on left and right operands and c //= a, equivalent to c = c // a
assign the result to left operand
Python allows simple syntax for assigning same value to multiple variables as
follows (chained assignment):
x=y=z=12
12 is assigned to x,y and z
a & b = 1010
&
0100
= 0000
= 0 (Decimal)
Bitwise operators
• 2) Bitwise OR operator:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a | b = 1010
|
0100
= 1110
= 14 (Decimal)
Bitwise operators
• Bitwise XOR:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a ^ b = 1010
^
0100
= 1110
= 14 (Decimal)
Bitwise shift operator:
• These operators are used to shift the bits of a number left or right
thereby multiplying or dividing the number by two respectively. They
can be used when we have to multiply or divide a number by two.
Bitwise operators
• Bitwise right shift operator: Shifts the bits of the number to the right
and fills 0 on voids left as a result.
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5
Bitwise operators
• Bitwise left shift operator: Shifts the bits of the number to the left and
fills 0 on voids right as a result.
Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
Problem:
Take two integers x and y as inputs from the console using input() function. For
each bitwise operator ( >> , <<, &, |, ~, and ^ ), print to the console, the result of
applying these operators on the two input integers as shown in the example.