Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:23:08+00:00 2026-06-04T12:23:08+00:00

I wonder, how can I increment a string representing a binary number, all the

  • 0

I wonder, how can I increment a string representing a binary number, all the way up to another binary number? (for use in a while loop, for example).

For example I start with “0000” and after 15 incs, I should reach “1111” (in other words: “0000”, “0001”, “0010”, …, “1111”). At first this problem seemed really simple, but the only solutions I could come up with were quite ridiculous (not pythonic, some might say). Does anyone have an advice?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:23:09+00:00Added an answer on June 4, 2026 at 12:23 pm

    To do what you literally asked for you should convert to an integer, add one, then change back to binary.

    x = to_binary(int(x, 2) + 1)
    

    You may find the bin built-in method useful in implementing to_binary, though you will need to modify its output slightly to match your desired result.

    However it would be better if possible to not convert back and forth: just store an integer and convert it to binary when you need to display it.

    On reading your question carefully though, it seems that all you want to do is to generate the strings '0000', '0001', '0010', etc. If this is correct, then I suggest using itertools.product.

    Example:

    import itertools
    for x in map(''.join, itertools.product('01', repeat=4)):
        print x
    
    0000
    0001
    0010
    ...
    

    See it working online: ideone

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder how can i remove all string part after #. For example if
I'd like to start coding for NVIDIA 3D Vision and wonder where can I
I wonder how can I reconcile the following error? JVM cannot use large page
I wonder how can i hide the attached panel in a way that only
I wonder how can I use similar to Eclipse's remote debugging technique to get
I'm making many graphs which have same xlab. I wonder can we use xlab
I wonder how can I export a Visual Studio C++ project to Qt? I
I wonder how can I search for a certain pattern in file, but only
Out of curiosity, I wonder what can people do with parsers, how they are
wonder whether someone can help me with the following one... I have a struct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.