Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use binary Operation to find the Sum and difference of two numbers by python

Shulou Source: shulou.com Published: 2022-06-01 13:05:09 09月29日 Update

This article mainly introduces the relevant knowledge of "how python uses binary operation to find the sum and difference of two numbers". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "how python uses binary operation to find the sum and difference of two numbers" can help you solve the problem.

The following is the use of functions in python to find the sum and difference of two numbers class Calculate: def-- init-- (self,number1 Number2) # receives two parameters self.number1 = number1 self.number2 = number2 def he (self): print ("and is:% d"% (self.number1 + self.number2)) # to format the output def cha (self): print ("difference is:% d"% (self.number1-self.number2)) result = Calculate (5) # pass two parameters result.he () result.cha () # call the function to evaluate

Add: python_ writes a function to find the sum of two integers, requiring that the +, -, *, / four operation symbols should not be used in the function body.

Look at the code to write a function to find the sum of two integers, requiring that the +, -, *, / four operation symbols should not be used in the function body.'' # using XOR and carry to solve # cannot be a positive number and a negative number # may be the reason why the integer type of python can be infinite, which causes the XOR operations of positive and negative numbers to become smaller negative numbers without overflow # after a try with Swift, you can still get the # #-coding:utf-8-*-# class Solution:# def Add of the bit operation addition operation of positive and negative numbers (self Num1, num2): # while num2:# sum = num1 ^ num2# carry = (num1 & num2) 31 = 0 else num1-4294967296 method-return num1 if num1

Tags: Two operations functions negative numbers binary positive numbers knowledge symbols four operations sum parameters four operations integers methods industries utf-8 different practical continuous code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux MariaDB Microsoft Shulou Technology