addition

Python Example to sum of two integer using Bitwise operator

Python Example to sum of two integer using Bitwise operator

In this article, we will discuss the concept of the Python Example to sum of two integers using Bitwise operator

In this post, we are going to learn how to  write a program to find the sum of two numbers using Bitwise operator in Python programming language

Sum of two integer using Bitwise operator

Code to find the addition of  two numbers 

Addition of two integer using Bitwise operator

The program allows the user to enter two integers and then calculates sum of given  numbers using  Bitwise operator in Python language

Program 1

#Python program to sum of two numbers
#using bitwise operator
a=int(input("Enter the number for a: "))
b=int(input("Enter the number for b: "))
#ask input from the user
while(b != 0):
#calculate sum of two integer using bitwise operator
        c=a&b #and operator

        a=a^b #Xor operator

        b=c<<1
    
    
print("Sum of two numbers",a)
#display output on the screen

When the above code is executed, it produces the following result

Enter the number for a: 130
Enter the number for b: 220
('Sum of two numbers', 350)

 

Sum oft two integer using Bitwise operator – using function

The program allows the user to enter two integers and then calculates sum of given  numbers using  Bitwise operator in Python language

Program 2

#Python program to sum of two numbers
#using bitwise operator
a=int(input("Enter the number for a: "))
b=int(input("Enter the number for b: "))
#ask input from the user
def add(a,b):#function definition
    while(b != 0):
#calculate sum of two integer using bitwise operator
        c=a&b #and operator

        a=a^b #Xor operator

        b=c<<1
    return a

    
    
print("Sum of two numbers",add(a,b))
#display output on the screen

When the above code is executed, it produces the following result

Enter the number for a: 123
Enter the number for b: 234
('Sum of two numbers', 357)

 

Suggested for you

Operator in Python language

Data type in Python language

if statements in Python language

Function in Python language

 

Similar post

Subtract two numbers in  Python  language

Subtract two numbers using function in Python language

Find sum of two numbers in Python Language

Find sum of two numbers in Python using recursion

Find sum of two numbers in Python without Arithmetic operator

Find sum of natural numbers in Python  language

Find sum of natural numbers in Python language using recursion

C++ code to sum of two integer using Bitwise operator
Program to sum of two integer using without + operator in Java
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago