Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python Numpy Bitwise operators

by suresh

The Python Numpy Bitwise operators and Functions used to perform bitwise operations. They are bitwise_and, &, bitwise_or, |, invert (bitwise not), left_shift, <<, right_shift and >>. These Python Numpy Bitwise operators compare the binary representation of both the values and return the output.

You can also use these Python Numpy Bitwise operators and Functions as the comparison operators. I mean, comparing each item against a condition. 

Python Numpy bitwise and

The Python Numpy bitwise and operator, bitwise_and function returns True, if both bit values return true otherwise, False. Before we get into the practical example, let me show you the Truth table behind this bitwise and using the below Python program.

import numpy as np
 
print('----Bitwise and operator Example----')
print('True & True   = ', (True & True))
print('True & False  = ', (True & False))
print('False & True  = ', (False & True))
print('False & False = ', (False & False))
       
print('\n----bitwise_and Function Example----')
print('True bitwise_and True   = ', np.bitwise_and(True, True))
print('True bitwise_and False  = ', np.bitwise_and(True, False))
print('False bitwise_and True  = ', np.bitwise_and(False, True))
print('False bitwise_and False = ', np.bitwise_and(False, False))

OUTPUT

Python Numpy Bitwise and 1

In this example, we declared two integer variables and used the bitwise and on them. First, it converts them to binary format and then compares each bit of a against b. Here, 12 = 00001100 and 25 = 00011001 so, 00001100 & 00011001 retunes 00001000 means 8.

import numpy as np
 
a = 12
b = 25
 
print('Binary Value of 12 = ', bin(a))
print('Binary Value of 25 = ', bin(b))
 
print('\nBinary Value of 12 = ', np.binary_repr(a))
print('Binary Value of 25 = ', np.binary_repr(b))
 
print('\nBitwise and Operator Result = ', a & b)
print('bitwise_and Function Result = ', np.bitwise_and(a, b))

OUTPUT

Python Numpy Bitwise and 2

In the above Python example, we used this Numpy bitwise_and on single values. Let me try this bitwise and operator and function on two arrays. It means the individual item in arr1 performs binary and with arr2 and returns the bitwise_and result.

import numpy as np
 
arr1 = np.array([2, 12, 9, 12, 17, 11])
print(arr1)
arr2 = np.array([14, 13, 65, 25, 42, 65])
print(arr2)
 
print('\nNumpy Bitwise and Operator Result = ', (arr1 & arr2))
print('Numpy bitwise_and Function Result = ', np.bitwise_and(arr1, arr2))
print()
 
x = np.random.randint(1, 20, size = (3, 7))
print(x)
y = np.random.randint(50, 125, size = (3, 7))
print(y)
 
print('\n---Numpy Bitwise and Operator Result---\n', (x & y))
print('---Numpy bitwise_and Function Result---\n ', np.bitwise_and(x, y))

OUTPUT

Python Numpy Bitwise and 3

Until now, we are using Numpy bitwise and performing bitwise operations. However, you can use them to perform comparison operations as well. Let me use this bitwise and to check whether the item is is greater than 1 and less than 9. If the condition is True, then True is returned otherwise, false.

import numpy as np
 
arr = np.array([2, 5, 0, 7, 8, 4, 10, 9, 0, 6, 1, 11])
print(arr)
 
print('--- Numpy Bitwise and Operator ---')
print((arr > 1) & (arr < 9))
 
print('--- Numpy bitwise_and Function ---')
print(np.bitwise_and((arr > 1), (arr < 9)))

OUTPUT

Python Numpy Bitwise and 4

Here, we are performing the comparison on multi-dimensional arrays using bitwise_and function and bitwise and operator.

import numpy as np
 
arr = np.random.randn(3, 5)
print(arr)
 
print('--- Two Dimensional Numpy Bitwise and Operator ---')
print((arr > 0) & (arr < 0.5))
 
print('--- Two Dimensional Numpy bitwise_and Function ---')
print(np.bitwise_and((arr > 0), (arr < 0.5)))
 
arr2 = np.random.randint(1, 25, size = (2, 2, 4))
print('--- Three Dimensional Random Array ---')
print(arr2)
 
print('--- Three Dimensional Numpy Bitwise and Operator ---')
print((arr2 > 5) & (arr2 < 20))
 
print('--- Three Dimensional Numpy bitwise_and Function ---')
print(np.bitwise_and((arr2 > 5), (arr2 < 20)))

OUTPUT

Python Numpy Bitwise and 5

Python Numpy bitwise or

The Python Numpy bitwise or operator and bitwise_or function return False if both bit values return False otherwise, true. Let me show you the bitwise or Truth table using the below program.

import numpy as np
 
print('----Bitwise or operator Example----')
print('True | True   = ', (True | True))
print('True | False  = ', (True | False))
print('False | True  = ', (False | True))
print('False | False = ', (False | False))
       
print('\n----bitwise_or Function Example----')
print('True bitwise_or True   = ', np.bitwise_or(True, True))
print('True bitwise_or False  = ', np.bitwise_or(True, False))
print('False bitwise_or True  = ', np.bitwise_or(False, True))
print('False bitwise_or False = ', np.bitwise_or(False, False))

OUTPUT

Python Numpy Bitwise or 1

Here, we declared two int variables and used the bitwise or and bitwise_or on those two. As we already know, 12 = 00001100, 25 = 00011001 so, 00001100 | 00011001 retunes 00011101 means 29.

import numpy as np
 
a = 12
b = 25
 
print('Binary Value of 12 = ', np.binary_repr(a))
print('Binary Value of 25 = ', np.binary_repr(b))
 
print('Bitwise or Operator Result = ', a | b)
print('bitwise_or Function Result = ', np.bitwise_or(a, b))

OUTPUT

Python Numpy Bitwise or 2

In this example, we are using Numpy bitwise_or function and bitwise or operator on two arrays.

import numpy as np
 
arr1 = np.array([2, 5, 0, 12, 8, 10])
print(arr1)
 
arr2 = np.array([12, 30, 6, 25, 12, 65])
print(arr2)
 
print('\nNumpy Bitwise or Operator Result = ', (arr1 | arr2))
print('Numpy bitwise_or Function Result = ', np.bitwise_or(arr1, arr2))
print()
 
x = np.random.randint(1, 15, size = (3, 5))
print(x)
y = np.random.randint(10, 25, size = (3, 5))
print(y)
 
print('\n---Numpy Bitwise or Operator Result---\n', (x | y))
print('---Numpy bitwise_or Function Result---\n ', np.bitwise_or(x, y))

OUTPUT

Python Numpy Bitwise or 3

Like bitwise and, you can use bitwise or and bitwise_or function to perform logical comparison as well. Let me use this bitwise or operator to check whether the item in an array is less than 3 or greater than 8. If either one of the condition is True, then True is returned otherwise, false.

import numpy as np
 
arr = np.array([2, 5, 0, 7, 8, 4, 10, 9, 0, 6, 1, 11])
print(arr)
 
print('--- Numpy Bitwise or Operator ---')
print((arr < 3) | (arr > 8))
 
print('--- Numpy bitwise_or Function ---')
print(np.bitwise_or((arr < 3), (arr > 8)))

OUTPUT

Python Numpy Bitwise or 4

Here, we used bitwise_or and bitwise or to perform the comparison on multi-dimensional arrays.

import numpy as np
 
arr = np.random.randn(3, 5)
print(arr)
 
print('--- Two Dimensional Numpy Bitwise or Operator ---')
print((arr < 0) | (arr > 1))
 
print('--- Two Dimensional Numpy bitwise_or Function ---')
print(np.bitwise_or((arr < 0), (arr > 1)))
 
arr2 = np.random.randint(1, 25, size = (2, 2, 4))
print('--- Three Dimensional Random Array ---')
print(arr2)
 
print('--- Three Dimensional Numpy Bitwise or Operator ---')
print((arr2 < 8) | (arr2 > 17))
 
print('--- Three Dimensional Numpy bitwise_or Function ---')
print(np.bitwise_or((arr2 < 8), (arr2 > 17)))

OUTPUT

Python Numpy Bitwise or 5

Python Numpy left shift

The Python Numpy left shift operator shifts the binary number towards the left side for a specified number of positions. For example, a<<1 or left_shift(a, 1) converts 12 to binary value and then left-shift one position. a = 00001100<<1 = 00011000 = 24. If a<<2, then 00110000.

import numpy as np
 
a = 12
b = 25
 
print('Binary Value of 12 = ', np.binary_repr(a))
print('Binary Value of 25 = ', np.binary_repr(b))
 
print('\nLeft Shift Operator Result = ', a << 1)
print('left_shift Function Result = ', np.left_shift(a, 1))
 
print('\nLeft Shift Operator Result = ', b << 1)
print('left_shift Function Result = ', np.left_shift(b, 1))

OUTPUT

Python Numpy left shift 1

In this example, we are using Numpy left_shift function and left shift operator on an array. The first statement (arr1 << 1) will perform binary left shift of one position on each and every item in an array. The second one (arr1 << arr2) perform left shift based on arr2 items. I mean, 2 << 1, 12 << 2, 9 << 3, 12 << 4, 17 << 5, 11 << 6

import numpy as np
 
arr1 = np.array([2, 12, 9, 12, 17, 11])
print(arr1)
 
arr2 = np.array([1, 2, 3, 4, 5, 6])
print(arr2)
 
print('\nNumpy Left Shift Operator Result = ', (arr1 << 1))
print('Numpy left_shift Function Result = ', np.left_shift(arr1, 1))
 
print('\nNumpy Left Shift Operator Result = ', (arr1 << arr2))
print('Numpy left_shift Function Result = ', np.left_shift(arr1, arr2))
print()
 
x = np.random.randint(1, 20, size = (3, 7))
print(x)
y = np.random.randint(1, 5, size = (3, 7))
print(y)
 
print('\n---Numpy Left Shift Operator Result---\n', (x << y))
print('---Numpy left_shift Function Result---\n ', np.left_shift(x, y))

OUTPUT

Python Numpy left shift 2

Python Numpy right shift

The Python Numpy right shift operator shifts the binary number towards right side for a given number of positions. For example b>>1 or right_shift(b, 1) converts 25 to binary value, next, right shift one position. b>>1 = 00011001>>1 = 00001100 = 12. 

import numpy as np
 
a = 12
b = 25
 
print('Binary Value of 12 = ', np.binary_repr(a))
print('Binary Value of 25 = ', np.binary_repr(b))
 
print('\nRight Shift Operator Result = ', a >> 1)
print('right_shift Function Result = ', np.right_shift(a, 1))
 
print('\nRight Shift Operator Result = ', b >> 1)
print('right_shift Function Result = ', np.right_shift(b, 1))

OUTPUT

Python Numpy right shift 1

Here, we are using Numpy right_shift function and right shift operator on an array. The first Numpy statement (arr1 >> 1) perform binary right shift of one position on each item in an array. The second one (arr1 >> arr2) perform right shift based on arr2 items. I mean, 2 >> 1, 12 >> 2, 9 >> 3, 12 >> 4, 17 >> 5, 11 >> 6

import numpy as np
 
arr1 = np.array([2, 12, 9, 12, 17, 11])
print(arr1)
 
arr2 = np.array([1, 2, 3, 4, 5, 6])
print(arr2)
 
print('\nNumpy right Shift Operator Result = ', (arr1 >> 1))
print('Numpy right_shift Function Result = ', np.right_shift(arr1, 1))
 
print('\nNumpy right Shift Operator Result = ', (arr1 >> arr2))
print('Numpy right_shift Function Result = ', np.right_shift(arr1, arr2))
print()
 
x = np.random.randint(1, 20, size = (3, 7))
print(x)
y = np.random.randint(1, 5, size = (3, 7))
print(y)
 
print('\n---Numpy right Shift Operator Result---\n', (x >> y))
print('---Numpy right_shift Function Result---\n ', np.right_shift(x, y))

OUTPUT

Python Numpy right shift 2

Python Numpy invert

The Python Numpy invert function is same as bitwise not operator. We show the same use the below-shown Python example.

import numpy as np
 
arr1 = np.array([10], dtype = np.uint8)
print(arr1)
 
print('Invert Value of arr1 = ', np.invert(arr1))
 
print('Binary Representation of arr1 = ', np.binary_repr(10, 8))
print('Binary Representation of x    = ', np.binary_repr(245, 8))
 
arr2 = np.array([1, 2, 3, 4, 5, 6], dtype = np.uint8)
print(arr2)
print('Invert Value of arr1 = ', np.invert(arr2))
 
for x in arr2:
    print('Binary Value of arr2  =', np.binary_repr(x, 8))
     
print('Binary Representation of 254 = ', np.binary_repr(254, 8))
print('Binary Representation of 253 = ', np.binary_repr(253, 8))
print('Binary Representation of 252 = ', np.binary_repr(252, 8))
print('Binary Representation of 251 = ', np.binary_repr(251, 8))
print('Binary Representation of 250 = ', np.binary_repr(250, 8))
print('Binary Representation of 249 = ', np.binary_repr(249, 8))

OUTPUT

Python Numpy invert

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy