944,131 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5454
  • Python RSS
Oct 22nd, 2007
0

Adding 1 with 1 in binary

Expand Post »
1+1 is 10 in binary and then you carry over the 1. I was wondering how to do that with Python? I was able to invert the binary and determine the sum of the rightmost bit and if it's 2, the rightmost digit would become 0. This is where I'm stuck at. I don't know how to carry over the 1. I was thinking of using the for function, and let it start from the right to the left using string indexing, but it gave me an error (TypeError: 'builtin_function_or_method' object is unsubscriptable).

This is part of the code that does what I just described.

Python Syntax (Toggle Plain Text)
  1. elif s < bit:
  2. oldbitstring = str(int('1'*(bit-s) + b))
  3. bitstring = str(int('1'*(bit-s) + b) + 1)
  4. if int(bitstring[-1]) == 2:
  5. oldbitstring = int(oldbitstring) - 1
  6. print oldbitstring

*note bit would be the number of bits the user inputs. s would be the length of the binary after the decimal is converted.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Agentbob is offline Offline
6 posts
since Oct 2007
Oct 22nd, 2007
0

Re: Adding 1 with 1 in binary

So the idea is, you have two binary numbers represented as strings, and then you want to add them?

I'm not seeing any obvious errors in the code above.
Last edited by jrcagle; Oct 22nd, 2007 at 8:44 pm.
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 22nd, 2007
0

Re: Adding 1 with 1 in binary

Click to Expand / Collapse  Quote originally posted by jrcagle ...
So the idea is, you have two binary numbers represented as strings, and then you want to add them?

I'm not seeing any obvious errors in the code above.
No, not exactly. What I"m trying to do is two bit complement.

So for example, I want to determine the two bit complement of 10 with 4 bits.

1010 would be the binary of 10.

1010 --> 0101
1<-----carry over 1, since 1+1 is 10 in binary terms.
0101
+ 1
----------
0

And so forth. That's what I'm trying to do with the code, but I'm stuck at the carrying over of the number 1.

**sorry, the addition table thing won't show up probably. That carried-over 1 is suppose to be above 0(the second digit from the right).
Last edited by Agentbob; Oct 22nd, 2007 at 8:56 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Agentbob is offline Offline
6 posts
since Oct 2007
Oct 22nd, 2007
0

Re: Adding 1 with 1 in binary

1. Make carry=1, and newBitString=""
2. Calculate sum of carry and last (rightmost) bit of the BitString.
If the sum equals to 2 then make lastBit=0 and carry=1;
otherwise make lastBit = sum and carry=0. Append lastBit
to the left of newBitString.
3. Repeat step 2 for the second to the last bit of the BitString, and
so on (namely for all bits from right to left).
4. Return newBitString as the result.

Here's the algorithm i'm trying to replicate. Adding 1 to a bitString.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Agentbob is offline Offline
6 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: newbie in need of help for hangman game
Next Thread in Python Forum Timeline: error in guessing game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC