Bitwise
Operators
Bitwise
operators treat their values as a sequence of bits (zeroes and ones) and
perform the bit-by-bit operation. For example, if we assign( a =10) number it’s
considered as (a =1010) in binary form. But result in we be normal
numbers. some types of bitwise operators on below
Operator |
Operator
Name |
Description |
Example |
'&' |
Bitwise
AND |
If
both values are ‘one’ then result also ‘one’ otherwise it’s zero |
1&0
= 0 |
'|' |
Bitwise
OR |
If
one value is ‘one’ then the result is ‘one’ otherwise it’s ‘zero’ |
1
|0 = 1 |
'^' |
Bitwise
XOR |
If
both values are different then the result is ‘one’ otherwise it’s zero |
1^0
=1, 0^0
=0, 1^1 =1 |
'~' |
Bitwise Complement |
||
'<<' |
Bitwise
Left Shift |
||
'>>' |
Bitwise
Right Shift |
Some
example:
The bitwise operator has only taken int type, Boolean type only. Another type will
give an error.
Now
we are checking with ‘float’ values
print(4.5
& 4.3)
Result
Traceback
(most recent call last):
File
"C:/Users/Surya/PycharmProjects/Project001/main.py", line 38, in
<module>
print(4.5
& 4.3)
TypeError:
unsupported operand type(s) for &: 'float' and 'float'
Now
we are checking with ‘string’ values
print('ramu'
& 'gopi')
Traceback
(most recent call last):
File
"C:/Users/Surya/PycharmProjects/Project001/main.py", line 38, in
<module>
print('ramu'
& 'gopi')
TypeError:
unsupported operand type(s) for &: 'str' and 'str'
Now
we are checking with ‘int’ values
print(10
& 13)
8
# Result
How?..
10
=
1010 #
if you want to check use the bin() function.
13
= 1101
This is the ‘AND’ ( ‘&’ ) operator.
The rule is: If one value is ‘one’ then the result is ‘one’ otherwise, it’s ‘zero’.
10 |
= |
1 |
0 |
1 |
0 |
13 |
= |
1 |
1 |
0 |
1 |
Result |
= |
1 |
0 |
0 |
0 |
print(0b1000)
8
# Result
Now
we are checking with ‘bool’ values
print(True &
True)
True
# Result
Now we going to check with the ‘OR’ ( ‘|’ ) type.
print(10|13)
15
# Result
How?..
10
= 1010 #
if you want to check use the bin() function.
13
= 1101
This is the ‘|’ ( ‘OR’) operator.
The rule is: If both values are different then the result is ‘one’ otherwise
it’s zero
10 |
= |
1 |
0 |
1 |
0 |
13 |
= |
1 |
1 |
0 |
1 |
Result |
= |
0 |
1 |
1 |
1 |
print(0b0111)
7
# Result
Now
we are checking with ‘bool’ values
print(True|False)
True
# Result
Now
we going to check with the ‘XOR’ or ‘^’ type.
print(10^13)
7
# Result
How?..
10
=
1010 #
if you want to check use the bin() function.
13
= 1101
This
is the ‘^’ or ‘XOR’ operator.
The
rule is: If both values are ‘one’ then result also ‘one’ otherwise, it’s
zero.
10 |
= |
1 |
0 |
1 |
0 |
13 |
= |
1 |
1 |
0 |
1 |
Result |
= |
1 |
1 |
1 |
1 |
print(0b1111)
15
# Result
Now
we are checking with ‘bool’ values
print(True ^ False)
True
# Result
Now we going to check
with ‘Ones Complement’ or ‘~’ type.
if we taking 4
results will be -5, check on below.
print(~4)
-5
# Result
How?..
In
memory, the int value takes a 32-bit representation. In python starting it
takes 32 bit, after that its increase as per our value.
Some
points of complement operator.
1. The most significant bit (MSB) acts as a sign bit. (or) starting bit act like a
sign bit.
‘0’
means ‘+’ or a positive number
‘1’
means ‘-’ or a positive number
2. ‘+’ or positive number represents going directly into memory.
3. ‘-’ or negative number represents taking in 2’s complement form.
4. 2’s complement form means.
1’s complement form, it’s like if it’s
taking 0,1 then the result will be 1,0.
This 1’s complement form.
2’s complement form, all bits are the
same as 1’s complement and the last bit is converted to one bit.
Now
taking our example of 4 number Binary signed complement form.
4
= 16 bit looks like.
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now
taking our example of ~4 number Binary signed complement form.
~4
= 16-bit table looks like.
4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
~4 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
1’s Complement | ‘-' | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
2’s Complement | ‘-' | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
Result | ‘-' | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
4
is a positive number so it’s taking starting the number is 0 and so on.
1st stage:
If it’s starting ‘o’ is the sign bit. All other bits going directly into
memory.
2nd stage:
in this stage, all bits are converted to complement form. In this form, all bits
are replaced with zero and one Based on the binary form. Check on the second
line.
3rd stage:
In this stage, all bits are converted to reverse. Based on complement form.
Except for the first bit. This form called 1’s complement. In this
stage, the first bit is ‘0’ which means the final result is a positive
number. ‘1’ means the final result is a negative number Check
on the third line.
4th
stage: In this state last bit only added ‘one’. Based on 1’s
complement.
Result
Stage: As per 1’s complement stage all bits taking a bit of
memory except the last bit. The last bit will be 2’s complement stage
last bit.
The Final is result is -5.
Shift
operator:
We
have two shift operators are there.
<< #
Left Shift operator
>>
# Right Shift operator
Left
Shift operator:
Ex:
10<<2
40
# Result
In
this example ‘10’ number convert to binary
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The
left shift operator removes the starting 2 bits. Like below
And
add two bits in last and added with ‘o’. Like below.
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now it’s converted to
left shift operator.
print(0b101000)
40 #
Result
Right Shift
operator:
Ex:
10>>2
2 #
Result
In this example
‘10’ number convert to binary
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The
right shift operator removes the last 2 bits. Like below
And add two bits in
First and added with the given value sign bit. For example, if we give a
positive number it takes an ‘o’ if we give a negative number it takes a ‘1’. In
our example, we take a positive number so it takes a ‘0’ sign bit.
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(0b10)
2 #
Result
We
can apply bitwise operators for Boolean types also, results are given below.
print(True &
False) #
Result is False
print(True | False)
#
Result is True
print(True ^ False)
# Result is
True
print(~True)
# Result is -2
print(True<<2)
# Result is
4
print(True>>2) #
Result is 0
Ternary operator
An operator having only one argument then it’s called a unary operator.
An operator has two arguments then it’s called a binary operator.
An operator having three arguments then it’s called a Ternary operator.
Ex:
a=10
b=20
c=30
if a>b else 40
ternary operator
syntax:
c = first_vlaue if
condition else second_vlaue.
Ex:01
a,b=10,20
min=a
if a<b else b
print(min)
10
# Result
Ex:02
a,b,c=10,20,30
min=a
if a<b and a<c else b if b<c else c
print(min)
10
# Result
Ex:03
a,b,c=10,20,30
max
=a if a>b and a>c else b if b>c else c
print(max)
30
# Result
Ex:03
a
= int(input('enter the number: ')) #This is most commely used in
any laguage.
b
= int(input('enter the number: '))
result
= 'Both numbers of equal' if a==b else 'First number is smaller, then second
number' if a<b else 'First number is bigger, then second number'
print(result)
enter
the number: 20
enter
the number: 20
Both
numbers of equal # Result
Ex:04
a
= int(input('enter the number: ')) #This is most commely used in any
laguage.
b
= int(input('enter the number: '))
result
= 'Both numbers of equal' if a==b else 'First number is smaller, then second
number' if a<b else 'First number is bigger, then second number'
print(result)
enter
the number: 40
enter
the number: 20
First
number is bigger, then second number # Result
Post a Comment