Assignment Operators and Bitwise Operators In python

 
Assignment Operators and Bitwise Operators In python

 What is an operator ?

 - operator is a Special Symbol which usually represents  an action or process on

    variables .An operator can manipulate an operand.

1.Assignment Operator :

 Name itself suggest that assigning value to operand .The in-place operator also known as shortcut operator .Assignment operators can be used on other data types as well .Assignment Operator is simply means a operator which is use to assign a value to variable
Assignment Operators Will help to reduce code and as well  as it make code classy 
Example    x=2   //  " = " is equal to is simple assignment operator who assign value 2 into                          variable x 
 


Operator

Example

Equivalent to..

=

x=2

x=2

+=

x+=1

x=x+1

-=

x-=2

x=x-2

*=

x*=3

x=x*3

/=

x/=4

x=x/4

%=

x%=5

x=x%5

**=

x**=2

x=x**2

//=

x//=1

x=x//1

&=

x&=7

x&7


Generally assignment operators will use in the questions to confuse student  



2. Bitwise Operator : 

   As the name suggests, bitwise operators perform operations on bit level . These operators include
bitwise AND <OR<XOR . bitwise operator cannot be applied to float or double.
Python has six Bitwise operators . Following table will help to understand in well 
    
Bitwise OperatorDescriptionSimple Example
&Bitwise AND Operator5 & 3= 1
|Bitwise OR Operator5 | 3= 7
^Bitwise XOR Operator5 ^ 3 = 6
~Bitwise Ones’ Complement Operator~5 = -6
<<Bitwise Left Shift operator5<<3 = 40
>>Bitwise Right Shift Operator10>>1 = 2

1.Truth table of Bitwise AND operator :

A

B

A&B

0

0

0

0

1

0

1

0

0

1

1

1

 
2.Truth table of Bitwise OR Operator   : 

A

B

A|B

0

0

0

0

1

1

1

0

1

1

1

1


3. Truth table of Bitwise XOR    :


A

B

A^B

0

0

0

0

1

1

1

0

1

1

1

0


4.Bitwise NOT:

A

~A

0

1

1

0

Shifting operators :

Python supports two shifting operator . they are right shift and left shift . These operators are used
 to shifts bits either  to the left side or right side. 
1 . Left shift Operator " << " = Now to explain left shift consider above example

                                                      
                                                  5<<3 Here Binary Digits of 5 get shift by 3
                                                  5 = 0101.0000 (Binary ) get shifted by 3 digits on LHS
                                                        0101000.00 = 40   (Decimal )


In left Shit we Gain the bits


2. Right Shift Operator ">>"= 10 Binary Representation (1010)  Get shifted By three                                                        
                                                      Digits  on right hand side 10>>3
                                                     
                                                      1.01000 whos decimal value is 1
             
In Right Shift we Lose the Bits . an if use notice carefully you will notice that shift once to right is divides the number by 2.Hence multiple shifts to the right , result in dividing the number by 2.

Note : to understand shifting operator Consider a  decimal point