What does the & operator do?

Hi everyone,
This is probably an easy question but I didn’t find any explanation on the net. I can’t understand waht the & operator do when applied to tensors like below:

a = torch.from_numpy(np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]))
print(a,'\n')
b = torch.from_numpy(np.array([[0,10,10],[10,10,10],[7,8,9],[0,10,12]]))
print(b,'\n')
print(a & b,'\n')
print(b & a)

For this test program I get the following output which I can’t understand:

 tensor([[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9],
        [10, 11, 12]]) 

 tensor([[ 0, 10, 10],
        [10, 10, 10],
        [ 7,  8,  9],
        [ 0, 10, 12]]) 

 tensor([[ 0,  2,  2],
        [ 0,  0,  2],
        [ 7,  8,  9],
        [ 0, 10, 12]]) 
 
 tensor([[ 0,  2,  2],
        [ 0,  0,  2],
        [ 7,  8,  9],
        [ 0, 10, 12]])

Thank you in advance for your explanations!

Hi,

You can find it in the “python bitwise operators” section here.
tldr: bitwise AND.

1 Like