# Numpy Binary operations

Numpy provides various routines to perform bit by bit operation. These operations are performed on the corresponding bits of the binary representation of the operands. The following routines are available for binary operations.

## numpy.bitwise_and

This method calculates the bit-wise AND of two arrays entry-wise. For example,

```import numpy as np
a = np.array([[10,12],[14,16]])
b = np.array([[2,4],[6,8]])
print("Bitwise-and of a and b: \n",np.bitwise_and(a,b))

#Output
Bitwise-and of a and b:
[[2 4]
[6 0]]
```

## numpy.bitwise_or

This method calculates the bit-wise OR of two arrays entry-wise.

```import numpy as np
a = np.array([[10,12],[14,16]])
b = np.array([[2,4],[6,8]])
print("Bitwise-OR of a and b: \n",np.bitwise_or(a,b))

#Output
Bitwise-OR of a and b:
[[10 12]
[14 24]]
```

## numpy.bitwise_xor

This method calculates the bit-wise XOR of two arrays entry-wise. For example,

```import numpy as np
a = np.array([[10,12],[14,16]])
b = np.array([[2,4],[6,8]])
print("Bitwise-XOR of a and b: \n",np.bitwise_xor(a,b))

#Output
Bitwise-XOR of a and b:
[[ 8  8]
[ 8 24]]
```

## numpy.invert

This method calculates bit-wise inversion, or bit-wise NOT, entry-wise. For example,

```import numpy as np
a = np.array([[10,12],[14,16]])
print("Bitwise-invert of a: \n",np.invert(a))

#Output
Bitwise-invert of a:
[[-11 -13]
[-15 -17]]
```

## numpy.left_shift

This method is used to shift the bits of an integer to the left. For example,

```import numpy as np
a = np.array([[10,12],[14,16]])
print("Bitwise-left-shift of a: \n",np.left_shift(a, 2))

#Output
Bitwise-left-shift of a :
[[40 48]
[56 64]]
```

## numpy.right_shift

This method is used to shift the bits of an integer to the right. For example,

```import numpy as np
a = np.array([[10,12],[14,16]])
print("Bitwise-right-shift of a: \n",np.right_shift(a, 1))

#Output
Bitwise-right-shift of a:
[[5 6]
[7 8]]
```

# Bitwise Output formatting

## binary_repr

This method returns the binary representation of the input number as a string. The syntax of the method is,

```binary_repr(num[, width])
```

Here num int and only an integer decimal number can be used.

Width type is int, and it is optional. The length of the returned string if num is positive, or the length of the two’s complement if num is negative, provided that width is at least a sufficient number of bits for num to be represented in the designated form.

For example,

```import numpy as np
print("Binary repr of a \n", np.binary_repr(5))
print("Binary repr of a \n", np.binary_repr(5, width=4))

#Output
Binary repr of a
101
Binary repr of a
0101
```