Monday, 27 May 2019

Bitwise AND Operator (&)

Bitwise AND Operator(&):

This operator performs AND operation on the individual bits of numbers. The symbol for this operator is ‘&’, which is called ampersand.


The AND gate circuit present in the computer, the chip will perform the AND operation.





Bitwise operators are applicable only  for int and Boolean types.
By mistake, if we are trying to apply for any other type then we will get error.
print(4&5)-àValid
print(2.5&6.2)-àError:
print(2.3&2) -àError
>>> print(True&True)
True
>>> print(True&False)
False
>>> print(2&6)
2

2
2

2
1
0
2
0
1

Read the remainders from bottom to up
10 remaining 6 bits filled with 0s
i.e 000000 10

Read the remainders from bottom to up
Binary form of 6 is 110 (remaining 5 bits are filled with 0s) i.e
00000111
A&b is:
A:-à  0 0 0 0 0 0 1 0
B:à   0 0 0 0 0 1 1 1
-----------------
A&b= 0 0 0 0 0 0 1 0
------------------------

Now we need to convert from binary to decimal:
0
0
0
0
0
0
1
0
27
26
25
24
23
22
21
20
128
64
32
16
8
4
2
1
0
0
0
0
0
0
2
0
Add 1’s: 0+0+0+0+0+0+2+0=2
So result is 2


Steps to convert decimal to Binary
1.  Write down the decimal number ex: 6
2.  Divide the decimal number by 2
3.  Write the result underneath (below)

4.  
Write the remainder on the right hand side. This will be 0 or 1

5.  Divide the result of the division by 2 and again write down the remainder
 6.  Continue dividing and writing down remainders until the result of the division is 0

7.  Read the remainders from bottom to up

Steps to convert binary to decimal:
Converting from binary to decimal involves multiplying the value of each digit.
1.  Write down the number
2.  Multiply the digit by value place holder
3.  Continue doing this until you the number
Add the result together (only 1’s)

Bitwise AND Operator (&)

This operator performs AND operation on the individual bits of numbers. The symbol for  this operator is &, which is called ampersand. To understand the bitwise AND operation.

The AND gate circuit present in the computer chip will perform the AND operation.
a=10
b=11

Decimal number (Ex: 10) to Binary Conversion:
















1 byte is 8 bits so remaining are filled with 0’s
a=10 à0000 1010

b=11













b=11->0000 1011

So
a=10->0000 1010
b=11->0000 1011
-----------
a&b=  0000  1010 ..This is nothing but 10 (in decimal)           
-----
How do we convert from binary to decimal:

0
0
0
0
1
0
1
0
27
26
25
24
23
22
21
20
--
--
32
16
8
4
2
1
0
0
0
0
8
0
2
0=10

0+0+0+0+8+0+2=evaluation is right to left so 10
32..16..8..4..4..2..1 are weights

And Truth Table :

a
b
a&b
0
0
0
0
1
0
1
0
0
1
1
1

AND Gate:




Another way for convert from a decimal number to Binary:
Example if we take decimal number 10

Steps:
Divide this number by 2 and ignore the remainder, keep repeating this process till we get 1

(1.) 10 divide by 2 which gives 5
     5 <--10  <---

(2.) Divide by 2 we get 2, ignore remainder 1
   i.e 2<-- 5 <--10

(3.) 2 divide by 2 we get 1, ignore remainder 0
   i.e 1<-- 2<-- 5<-- 10

(4.) Now we are going to write 1 below any of the odd number
   and 0 below any of the even number.
   i.e write 1 for odd numbers; 0 for even number
          1  2  5  10
   i.e   1  0  1    0
so 10=1010 (In binary)





Next Topic: Bitwise OR Operator (|)
Previous Topic: Bitwise Operators

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...