Python Forum
Bitwise Operations in numpy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Bitwise Operations in numpy (/thread-41892.html)



Bitwise Operations in numpy - Sowmya - Apr-03-2024

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


RE: Bitwise Operations in numpy - deanhystad - Apr-03-2024

1 & 5 = 0001 & 0101 = 0001 = 1
2 & 6 = 0010 & 0110 = 0010 = 2
3 & 7 = 0011 & 0111 = 0011 = 3
4 & 8 = 0100 & 1000 = 0000 = 0


RE: Bitwise Operations in numpy - Sowmya - Apr-03-2024

But I have a doubt how you got this 0001 & 0101 = 0001 = 1


RE: Bitwise Operations in numpy - deanhystad - Apr-03-2024

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