471,338 Members | 1,509 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

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=00110100000000000001111111111111110000000000 00000001111111010111111111111001
str(bit)
'9494561295743363139170391117076063684345104265935 32217946399871489'

I would appreciate a prompt reply because I have a python assessment to
submit.
Thanks,
Thomas
Dec 15 '07 #1
8 3437
On Dec 15, 2:33 pm, te...@york.ac.uk 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

Dec 15 '07 #2
>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.

Dec 15 '07 #3
On 2007-12-15, te***@york.ac.uk <te***@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...
Dec 15 '07 #4
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=0011010000000000000111111111111111000000000000 000001111111010111111111111001
>>>str(bit)
'9494561295743363139170391117076063684345104265935 32217946399871489'
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
Dec 15 '07 #5
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

Dec 15 '07 #6
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
Dec 15 '07 #7
On Dec 15, 10:39�am, te...@york.ac.uk 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
Dec 15 '07 #8
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("Enter the sequence of 1 and 0: ") print inp
1001101011101011101
>>>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.
>>'1001101011101011101'.count('1')
12

--
Steven
Dec 16 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sam Smith | last post: by
15 posts views Thread by Kueishiong Tu | last post: by
4 posts views Thread by QQ | last post: by
9 posts views Thread by Simple Simon | last post: by
7 posts views Thread by farnaz.shahed | last post: by
7 posts views Thread by anuragkhanna8 | last post: by
2 posts views Thread by runcyclexcski | last post: by
8 posts views Thread by Hahnemann | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.