How to Use Bitwise AND Operator in Python


 1. Introduction

In this tutorial, let us see how to use Bitwise AND Operator in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

a =  5 # i.e binary 0101
b =  9 # i.e binary 1001 
z = a & b # ie. binary 0001 (=1)
print('%d & %d = %d' %(a, b, z))

a =  42 # i.e binary 101010 
b =  43 # i.e binary 101011
z = a & b # ie. binary 101010 (=32+8+2)
print('%d & %d = %d' %(a, b, z))

a =  42 # i.e 101010
b =  44 # i.e 101100
z = a & b # i.e. binary 101000 (=32+8)
print('%d & %d = %d' %(a, b, z))

Output:

5 & 9 = 1
42 & 43 = 42
42 & 44 = 40
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement