Convert a sequence of bits to a bit-string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • te509@york.ac.uk

    Convert a sequence of bits to a bit-string


    Hi guys,
    does anybody know how to convert a long
    sequence of bits to a bit-string? I want to avoid this:
    >>bit=001101000 000000000011111 111111111100000 000000000000111 111101011111111 1111001
    >>str(bit)
    '94945612957433 631391703911170 760636843451042 659353221794639 9871489'

    I would appreciate a prompt reply because I have a python assessment to
    submit.
    Thanks,
    Thomas


  • Arnaud Delobelle

    #2
    Re: Convert a sequence of bits to a bit-string

    On Dec 15, 2:33 pm, te...@york.ac.u k wrote:
    Hi guys,
    Hi
    does anybody know how to convert a long
    sequence of bits to a bit-string?
    Yes!
    >
    I would appreciate a prompt reply because I have a python assessment to
    submit.
    Good luck, you're lucky you've got the whole weekend :)
    Thanks,
    Thomas
    HTH

    --
    Arnaud

    Comment

    • te509@york.ac.uk

      #3
      Re: Convert a sequence of bits to a bit-string

      >does anybody know how to convert a long
      >sequence of bits to a bit-string?
      >
      >Yes!
      Would you like to help, please?
      >I would appreciate a prompt reply because I have a python assessment to
      >submit.
      >
      >Good luck, you're lucky you've got the whole weekend :)
      That's not the only assignment I have to do...
      >Thanks,
      >Thomas
      >
      >HTH
      I hope this is not like RTFM cause as much as I searched on the web I
      couldn't find an answer.

      Comment

      • Grant Edwards

        #4
        Re: Convert a sequence of bits to a bit-string

        On 2007-12-15, te509@york.ac.u k <te509@york.ac. ukwrote:
        >>does anybody know how to convert a long sequence of bits to a
        >>bit-string?
        >>
        >>Yes!
        >
        Would you like to help, please?
        You'll have to define 'sequence of bits' and 'bit string' for
        us. The example you showed didn't really make sense in the
        context of those two phrases (neither of which has any sort of
        commonly accepted meaning in Python). You showed code that
        converted a string to an integer. That is apparently not what
        you wanted, but it's unclear what you do want.
        >>I would appreciate a prompt reply because I have a python
        >>assessment to submit.
        >>
        >>Good luck, you're lucky you've got the whole weekend :)
        >
        That's not the only assignment I have to do...
        I hope this is not like RTFM cause as much as I searched on
        the web I couldn't find an answer.
        I'm not sure what the class is, but perhaps you're not supposed
        to "find an answer" on the web. Perhaps you're supposed to
        think up an answer yourself?

        In any case, I'm surprised you didn't find an answer, because
        the answer to the question I suspect you're trying to ask comes
        up quite regularly in this newsgroup.

        Here's a hint to get you started:

        help(int)

        --
        Grant Edwards grante Yow! I hope I
        at bought the right
        visi.com relish... zzzzzzzzz...

        Comment

        • Steven D'Aprano

          #5
          Re: Convert a sequence of bits to a bit-string

          On Sat, 15 Dec 2007 14:33:04 +0000, te509 wrote:
          Hi guys,
          does anybody know how to convert a long sequence of bits to a
          bit-string?
          Can you explain what you actually mean?

          For instance...

          x = 1234567890

          x is now a long sequence of bits.

          I want to avoid this:
          >
          >>>>
          bit=00110100000 000000001111111 111111110000000 000000000011111 110101111111111 11001
          >>>str(bit)
          '94945612957433 631391703911170 760636843451042 659353221794639 9871489'
          Rather than telling us what you DON'T want, perhaps you should tell us
          what you DO want.

          Also, bit (singular) is a bad name for something which is obviously many
          bits (plural).

          00110... [many digits skipped] is a very large number written in octal
          (base 8). It contains 220 bits, regardless of whether you print them in
          base 2, base 8, base 10 or base 256.


          I would appreciate a prompt reply because I have a python assessment to
          submit.
          Thank you for your honesty.

          You should think more carefully about what you are trying to accomplish.
          Is your input a string of 0 and 1 characters, or a binary sequence of
          bits? What is your output?

          e.g. bits = '010011001' # a sequence of characters representing bits
          bits = 987654 # bits specified in base 10

          (2) Python has built-in functions oct() hex() str() and int(). You may
          like to Read The Fine Manual using the built-in help.

          e.g. help(oct)
          Help on built-in function oct in module __builtin__:

          oct(...)
          oct(number) -string

          Return the octal representation of an integer or long integer.



          (3) Once you've explained what you're trying to do, think more carefully
          about how to accomplish it.

          e.g. the decimal string '123' is equal to 1*10**2 + 2*10**1 + 3*10**0.
          the octal string '406' is equal to 4*8**2 + 0*8**1 + 6*8**0
          the binary string '101' is equal to ... ?


          (4) You might also like to consider the words "convert from one base to
          another" as worthy of further research.


          Good luck on your assignment. Don't forget to quote your sources,
          including any code snippets you find on the web.



          --
          Steven

          Comment

          • te509@york.ac.uk

            #6
            Re: Convert a sequence of bits to a bit-string

            First of all I'd like to thank you all for your advices. Before I check all
            your hints I want to clarify what I'm trying to do. The question says
            "write a function that takes a sequence of bits as an input and return how
            many 1s are in the sequence", nothing more. This is quite simple for string
            inputs but tricky for "number inputs". I've notice that if I try to convert
            a number starting with 0 to a string using str(), then I take a string
            representing another number (probably converted to decimal). So what I want
            to do is to write a generic version of a function that takes as an input a
            sequence of 1s and 0s in any format. The only way I can think to achieve
            that is by converting the "number inputs" to a string and then using the
            count() function. Thomas

            Comment

            • Steven D'Aprano

              #7
              Re: Convert a sequence of bits to a bit-string

              On Sat, 15 Dec 2007 16:39:32 +0000, te509 wrote:
              So what I want
              to do is to write a generic version of a function that takes as an input
              a sequence of 1s and 0s in any format.
              Given that there is an infinite number of possible formats, I suggest you
              lower your sights to something more practical.


              --
              Steven

              Comment

              • mensanator@aol.com

                #8
                Re: Convert a sequence of bits to a bit-string

                On Dec 15, 10:39�am, te...@york.ac.u k wrote:
                First of all I'd like to thank you all for your advices. Before I check all
                your hints I want to clarify what I'm trying to do. The question says
                "write a function that takes a sequence of bits as an input and return how
                many 1s are in the sequence", nothing more.
                Except that there is no such thing in Python
                as there is no binary representation. You could
                enter a sequence of characters, where each character
                represents a bit, such as s='1111' for 15.
                This is quite simple for string
                inputs but tricky for "number inputs". I've notice that if I try to convert
                a number starting with 0 to a string using str(), then I take a string
                representing another number (probably converted to decimal). So what I want
                to do is to write a generic version of a function that takes as an input a
                sequence of 1s and 0s in any format.
                That's probably not what you want. You don't want
                to enter 1's and 0's in any format, you want to
                accept a number in any format. Remember, the format
                does not change the number (assuming you always use
                the correct format representation) .

                So if the input is s=017 (octal), the number
                is fifteen. If s=0xf (hexadecimal), the number
                is fifteen. If s=15 (decimal), the number is
                fifteen.

                Once you've got that straight, you can calculate
                the base 2 representation of fifteen and count
                the ones.
                The only way I can think to achieve
                that is by converting the "number inputs" to a string and then using the
                count() function.
                Do you know how base conversion is done manually?

                In base 2, each bit represents a power of 2, and
                there are only two possibilities for each digit,
                0 and 1. So, in base 2, the digit positions
                are

                ... 2**7 2**6 2**5 2**4 2**3 2**2 2**1 2**0
                128 64 32 16 8 4 2 1

                Now, for fifteen, what's the largest position
                that doesn't exceed 15? The fourth (counting
                from the right). Therefore, there's a 1 bit
                in position four and all higher positions
                would be 0. At this point, we have '1???'.

                To get the next lower position, subtract 8
                from fifteen, leaving seven. Now repeat
                until you fill all positions, eventually
                reaching '1111'.

                But, if the highest position that doesn't
                exceed skips some positions, then those
                positions have '0'.

                So for nine, the highest position is still
                the fourth, giving us '1???'. But after
                subtracting eight, we're left with one.

                But the highest position not exceeding one
                is the first, giving us '1??1'. We skipped
                positions 2 & 3, so they must be '0' making
                nine '1001' in base 2.

                Now all you have to do is count the 1's.

                However, if doing
                Thomas

                Comment

                • Steven D'Aprano

                  #9
                  Re: Convert a sequence of bits to a bit-string

                  On Sat, 15 Dec 2007 16:46:38 -0800, Dennis Lee Bieber wrote:
                  ... this becomes trivial... So trivial I'm going to include a solution,
                  even though it is a homework assignment...
                  >
                  >>>inp = raw_input("Ente r the sequence of 1 and 0: ") print inp
                  100110101110101 1101
                  >>>print "The count is %s" % len(
                  ... [c for c in inp if c == "1"] )
                  The count is 12

                  *cough*

                  It's even more trivial than that.
                  >>'100110101110 1011101'.count( '1')
                  12



                  --
                  Steven

                  Comment

                  Working...