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
    • Go Programs
    • Python Programs
    • Java Programs

Python Bitwise Operators

by suresh

Python Bitwise operators help perform bit operations. All the decimal values will convert into binary values (bits sequence i.e., 0100, 1100, 1000, 1001, etc.). Next, Python bitwise operators work on these bits, such as shifting left to right or transforming bit value from 0 to 1, etc.

The below table shows the different Python Bitwise operators and their meaning. For example, Consider x = 6 and y = 8 and their values in binary form are: x = 0110 and y = 1000

Python Bitwise OperatorsMeaning of operatorsExamples
&Bitwise ANDX & Y = 0000
|Bitwise ORX | Y = 1110
^Bitwise exclusive ORX ^ Y = 1110
~Bitwise complement~X = 00001001 (Bitwise Not operator will convert all 0 into 1.)
<<Shift leftX << 1 = 00001100 (Bits will move 1 step left. If we use 2 or 3 then they shift accordingly)
>>Shift rightY >> 1 = 00000100

The Truth Table behind Python Bitwise Operators is:

xyx & y X | yx ^ y
00000
01011
10011
11110

Python Bitwise Operators Example

In this bitwise operators example, we are using two variables a and b and their values are 9 and 65. Next, we use them to show you the list of Bitwise operations in Python Programming

>>> a = 9
>>> b = 65
>>> print("Bitwise AND Operator On 9 and 65 is = ", a & b)
>>> print("Bitwise OR Operator On 9 and 65 is = ", a | b)
>>> print("Bitwise EXCLUSIVE OR Operator On 9 and 65 is = ", a ^ b)
>>> print("Bitwise NOT Operator On 9 is = ", ~a)

>>> print("Bitwise LEFT SHIFT Operator On 9 is = ", a << 1)
>>> print("Bitwise RIGHT SHIFT Operator On 65 is = ", b >> 1)
Python Bitwise Operators 1

In this bitwise operators program, we declared 2 integers a and b and assigned the values 9 and 65. The binary form of 9 = 00001001 and 65 = 01000001.

>>> a = 9
>>> b = 65

Lets see the Python calculations of these Operators
Bitwise AND Operation = a & b
00001001 & 01000001 = 00000001 = 1

Bitwise OR Operation = a | b
00001001 | 01000001 = 01001001 = 73

The Bitwise Exclusive OR Operation in Python = a ^ b
00001001 ^ 01000001 = 01001000 = 72

Right Shift Operation in Python = b >> 1
01000001 >> 1 = 00100000 = 32

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 Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • 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

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy