Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add one to binary string
#2
Convert it to an int?

If this is a homework assignment, and you need to do the addition using the binary string, 1+1 = 0, carry the 1. You don't have a carry in you code.

I think it would also be easer working left to right instead of right to left. I would start by reversing the string "1010" to "0101", then I would do the addition, and finally I would reverse the string back.
Output:
1010 + 1 Reverse the strings 0101 +1 Do the addition 0+1 = 1 1+_ = 1 0+_ = 0 1+_ = 1 1101 Reverse the result 1011
If there is a carry
Output:
1010 + 10 Reverse the strings 0101 +01 Do the addition 0+0 = 0 1+1 = 0, carry = 1 0+_+carry = 1 1+_ = 1 0011 Reverse the result 1100
Reply


Messages In This Thread
Add one to binary string - by uwl - Sep-11-2022, 04:25 PM
RE: Add one to binary string - by deanhystad - Sep-11-2022, 05:01 PM
RE: Add one to binary string - by uwl - Sep-11-2022, 05:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [HELP] string into binary ZeroPy 2 2,114 Dec-31-2020, 08:15 PM
Last Post: deanhystad
  hex file to binary or pcap to binary baran01 1 5,761 Dec-11-2019, 10:19 PM
Last Post: Larz60+
  CSV file from Binary to String mr_byte31 2 13,665 Jul-27-2019, 08:46 PM
Last Post: snippsat
  HELP: String of Zero's and One's to binary byte schwasskin 1 3,917 May-19-2019, 07:31 AM
Last Post: heiner55
  binary search string help kietrichards 1 2,261 Mar-08-2019, 12:43 PM
Last Post: stullis
  converting binary b'0x31303032\n' to "1002" string amygdalas 2 2,698 Nov-07-2018, 03:50 AM
Last Post: amygdalas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020