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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago