Python Forum

Full Version: Bitwise Operations in numpy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all
I am learning Numpy class they said bitwise operations but did not understand how the out put come like this

import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])

print("a & b :",a&b)

Output

a & b : [1 2 3 0]

My doubt : What is the use of bitwise operators
How I got [1 2 3 0] this output
1 & 5 = 0001 & 0101 = 0001 = 1
2 & 6 = 0010 & 0110 = 0010 = 2
3 & 7 = 0011 & 0111 = 0011 = 3
4 & 8 = 0100 & 1000 = 0000 = 0
But I have a doubt how you got this 0001 & 0101 = 0001 = 1
It is doing a bitwise and. Looking at the bits in the two values and creating a new value that is only the bits that are set in both. Do you understand binary numbers?
Output:
int 1 = binary b0001 int 5 = binary b0101 1 & 5 = binary b0001 = 1