C. Swan
C. Swan

Reputation: 21

Adding binary numbers Python

I'm trying to add two binary numbers in python using a simple code. Can someone tell me why this isn't working? Thanks so much! I tried breaking the code down but when I break it down it all works separately. But of course when I put it together it doesn't work.

I'm a beginner in Python so explanations will certainly be appreciated. Thanks for the help!

#Enter numbers and define variables
number1=input("Enter 1st Binary Number:")
number2=input("Enter 2nd Binary Number:")
x1 = number1[0]
x2 = number1[1]
y1 = number2[0]
y2 = number2[1]

#Convert to integers from strings
int(x1)
int(x2)
int(y1)
int(y2)



#Digit 1 of answer and digit to be carried
if x1+y1==0:
  a1 = 0
  c1 = 0
if x1+y1==1:
  a1 = 1
  c1 = 0
if x1+y1==2:
  a1 = 0
  c1 = 1




#Digit 2 of answer and digit to be carried
if x2+y2+c1==0:
  a2=0
  c2=0
if x2+y2+c1==1:
  a2=1
  c2=0
if x2+y2+c1==2:
  a2=0
  c2=1
if x2+y2+c1==3:
  a2=1
  c2=1

#Digit 3 of answer
c2=a3

if a3==1:
  print ("a3" + "a2" + "a1")

if a3==0:
  print ("a2" + "a1")

The error it's giving is that c1 and a1 aren't defined.

Thanks again!

Upvotes: 1

Views: 3754

Answers (4)

void
void

Reputation: 2642

There are several ways in python to add two binary numbers and print them. Below are the methods. (However I will be explaining your problem below)

METHOD 1:

number1=input("Enter 1st Binary Number:")
number2=input("Enter 2nd Binary Number:")

num1 =int(number1,2)  //Convert string to binary number **NOTE:** int('string',base) is the format
num2 = int(number2,2)

bin_num = bin(num1+num2)
print(bin_num)
print(str(bin_num)[2:] #just to remove the '0b'

output:

Enter 1st Binary Number:10
Enter 2nd Binary Number:01
0b11
11

METHOD 2:

number1=input("Enter 1st Binary Number:")
number2=input("Enter 2nd Binary Number:")

num1 =int(number1,2)
num2 = int(number2,2)
print("{0:b}".format(num1+num2))

output:

Enter 1st Binary Number:10
Enter 2nd Binary Number:01
0b11
11

YOUR PROBLEM:

You can declare them at the top of your program and then use them.

Also you have one more thing wrong,

int(x1)
int(x2)
int(y1)
int(y2)

These four lines do nothing. You are not changing the values in x1,x2,y1,y2 you are just calling int() them without modifying them. Do this instead,

x1=int(x1)
x2=int(x2)
x3=int(y1)
x4=int(y2)

Complete code:

a1=c1=a2=c2=a3=c3=0 #Declare and define them all `0`s at top
#Enter numbers and define variables
number1=input("Enter 1st Binary Number:")
number2=input("Enter 2nd Binary Number:")
x1 = number1[0]
x2 = number1[1]
y1 = number2[0]
y2 = number2[1]

#Convert to integers from strings
x1=int(x1)
x2=int(x2)
x3=int(y1)
x4=int(y2)



#Digit 1 of answer and digit to be carried
if x1+y1==0:
  a1 = 0
  c1 = 0
if x1+y1==1:
  a1 = 1
  c1 = 0
if x1+y1==2:
  a1 = 0
  c1 = 1




#Digit 2 of answer and digit to be carried

if x2+y2+c1==0:
  a2=0
  c2=0
if x2+y2+c1==1:
  a2=1
  c2=0
if x2+y2+c1==2:
  a2=0
  c2=1
if x2+y2+c1==3:
  a2=1
  c2=1

#Digit 3 of answer
c2=a3


if a3==1:
  print (a3 , a2, a1,sep='')

if a3==0:
  print (a2 , a1,sep='')

And finally the last lines won't print the values of a1,a2 instead it will only print the strings. so remove the double quotes.

NOTE: I have given an extra parameter sep='' without it your output will have spaces between them. Like,

1 1

After sep='' you will get

11

Upvotes: 1

Supratim Samantray
Supratim Samantray

Reputation: 136

I will suggest simple code to add two binary numbers.

number1 = str(input("Enter 1st Binary Number"))
number2 = str(input("Enter 2nd Binary Number"))
intSum = int(number1, 2) + int(number2, 2)  #int() takes first argument as string
result = bin(intSum)[2:]  #remove 0b
print result

output

Enter 1st Binary Number1111
Enter 2nd Binary Number1010
11001

Upvotes: 3

RatDon
RatDon

Reputation: 3543

Every other answers are correct. Few other points though.

1.You can combine conversion to integer and assignment at one step. Like:

x1 = int(number1[0])
x2 = int(number1[1])
y1 = int(number2[0])
y2 = int(number2[1])

2.While doing multiple conditional checks with the same variable set, it's a good idea to use elif if it is not harming your logic. In your case:

#Digit 1 of answer and digit to be carried
if x1+y1==0:
  a1 = 0
  c1 = 0
elif x1+y1==1:
  a1 = 1
  c1 = 0
elif x1+y1==2:
  a1 = 0
  c1 = 1

Because, x1+y1==0, x1+y1==1, x1+y1==2 are not going to be true simultaneously. Similarly it can be applied for the digit 2 of the answer also.

3.The number of lines can be decreased with a pre-assignment.

a1 = c1 = 0

Then your digit 1 block will look like:

#Digit 1 of answer and digit to be carried
if x1+y1==1:
  a1 = 1
elif x1+y1==2:
  c1 = 1

Here, I removed the checking for x1+y1==0, because by default a1 and c1 are 0. And for other invalid cases, you have to make another conditional statement. Digit 2 block also can be modified similarly.

Upvotes: 0

hridayns
hridayns

Reputation: 697

Change

#Convert to integers from strings
int(x1)
int(x2)
int(y1)
int(y2)

to

#Convert to integers from strings
x1 = int(x1)
x2 = int(x2)
y1 = int(y1)
y2 = int(y2)

Also you have not defined a3:

#Digit 3 of answer
c2=a3

If you want to print the value of the variables (might have to convert them to strings using str()), use

if a3==1:
  print (a3 + " " + a2 + " " + a3)

if a3==0:
  print (a2 + " " + a1)

If you want to add the variables, use:

if a3==1:
  print (a3 + a2 + a1)

if a3==0:
  print (a2 + a1)

instead of

if a3==1:
  print ("a3" + "a2" + "a1")

if a3==0:
  print ("a2" + "a1")

Upvotes: 1

Related Questions