Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Bitwise Operators

Python

python logo

In this tutorial we will learn about Bitwise operators in Python.

We use bitwise operators in Python to work with bits i.e. 0s and 1s.

Following are the bitwise operators that we can use in Python.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR i.e. XOR
~One's complement
<< Shift left
>> Shift right

Bitwise AND

Bitwise AND & will give 1 only if both the operands are 1 otherwise, 0.

The truth table for bitwise AND.

ABA & B
000
010
100
111

In the following example we have two integer values 1 and 3 and we will perform bitwise AND operation and display the result.

# variables
x = 1
y = 3

# bitwise operation
result = x & y

print("result:", result)	# result: 1

Calculation of the bitwise AND operation for the above code.

# representing decimal in 32 bits (4 bytes)

  1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary)
& 3 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0011 (binary)
----------------------------------------------------------------
  1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary)

Click here to learn how to convert numbers from decimal to binary.

Bitwise OR

Bitwise OR | will give 0 only if both the operands are 0 otherwise, 1.

The truth table for bitwise OR.

ABA | B
000
011
101
111

In the following example we have two integer values 1 and 2 and we will perform bitwise OR operation and display the result.

# variables
x = 1
y = 2

# bitwise operation
result = x | y

print("result:", result)	# result: 3

Calculation of the bitwise OR operation for the above code.

// representing decimal in 32 bits (4 bytes)

  1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary)
| 2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
----------------------------------------------------------------
  3 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0011 (binary)

Bitwise XOR

Bitwise XOR ^ will give 1 for odd number of 1s otherwise, 0.

The truth table for bitwise XOR.

ABA ^ B
000
011
101
110

In the following example we have two integer values 2 and 7 and we will perform bitwise XOR operation and display the result.

# variables
x = 2
y = 7

# bitwise operation
result = x ^ y

print("result:", result)	# result: 5

Calculation of the bitwise XOR operation for the above code.

// representing decimal in 32 bits (4 bytes)

  2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
^ 7 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0111 (binary)
----------------------------------------------------------------
  5 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0101 (binary)

One's complement

One's complement ~ is a unary operator and it converts 0s to 1s and 1s to 0s.

In the following example we have an integer values 2 and we will compute its one's complement.

# variable
x = 2

# bitwise operation
result = ~x

print("result:", result)	# result: -3

Calculation for the above code.

// representing decimal in 32 bits (4 bytes)

2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
--------------------------------------------------------------
         ~2 = 1111 1111 1111 1111 1111 1111 1111 1101 (binary)
            = -3 (decimal)

We get -3 because we are working with signed integer number.

Shift Left

We use shift left << operator to shift the bits left.

In the following example we have an integer which we will left shift 1 position.

# variable
x = 4

# bitwise operation
result = x << 1

print("result:", result)	# result: 8

Calculation:

// representing decimal in 32 bits (4 bytes)

4 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0100 (binary)
--------------------------------------------------------------
     4 << 1 = 0000 0000 0000 0000 0000 0000 0000 1000 (binary)
            = 8 (decimal)

Shift Right

We use shift right >> operator to shift the bits right.

In the following example we have an integer which we will right shift 1 position.

# variable
x = 4

# bitwise operation
result = x >> 1

print("result:", result)	# result: 2

Calculation:

// representing decimal in 32 bits (4 bytes)

4 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0100 (binary)
--------------------------------------------------------------
     4 >> 1 = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
            = 2 (decimal)

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT