Subtraction of binary number in python by R4R Team

Binary number

-It is a number which expressed in the base-2 numeral system,
-Binary numbers are 0 or 1.

Subtraction of two binary number -


b1=1010 // 10 in decimal
b2=101 // 5 in decimal

then subtraction of b1 and b2 is 101 // 5 in decimal

program -

print("Enter first binary number")
print("Enter second binary number")

print("Subtraction of two binary number is - "+s)

output -

Enter first binary number
Enter second binary number
Subtraction of two binary number is - 0b101

Enter first binary number
Enter second binary number
Subtraction of two binary number is - -0b101

-In this program, firstly we take two input,
- int(n,2) derive the decimal equivalent so we find the decimal of both the number then subtract the second by first number.
- then using the bin() function we convert the decimal number into binary again. and this is the required ans.
- Here in second output negative binary number shows the -5 in decimal.

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!