Convert a sequence of bits to a bit-string

T

te509

Hi guys,
does anybody know how to convert a long
sequence of bits to a bit-string? I want to avoid this:
'949456129574336313917039111707606368434510426593532217946399871489'

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

Arnaud Delobelle

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
 
G

Grant Edwards

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.
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)
 
S

Steven D'Aprano

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:

'949456129574336313917039111707606368434510426593532217946399871489'

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.
 
M

mensanator

Hi guys,
does anybody know how to convert a long
sequence of bits to a bit-string? I want to avoid this:


'949456129574336313917039111707606368434510426593532217946399871489'

That's not a sequence of bits, it's a sequence of
octal digits (because you started with a leading 0).

There is no base 2 numeric format in Python.

You can enter strings reprsenting base 2 digits
and then convert the string to an integer:

Where you'll see what the correct decimal is:
15347835180116409679865L

As I said, the number you entered was actually octal.
Watch as I convert the string to base 8:
949456129574336313917039111707606368434510426593532217946399871489L

See, I got your original number.

You could change your original pattern from octal
to binary by substituting '000' for each '0' and
'001' for each '1'. This works because both octal
and binary are powers of 2.
'000000001001000001000000000000000000000000000000000000000001001001001001001001001001001001001001001001000000000000000000000000000000000000000000000000000001001001001001001001000001000001001001001001001001001001001001001000000001'

This string, when interpreted as base 2 gives
you the same result as the original number
interpreted as base 8.
949456129574336313917039111707606368434510426593532217946399871489L

If you really need full base 2 capability in place
of the halfway capability of Python, get the gmpy
module:
# convert int to base2 and pad
# to a fixed number of bits
print gmpy.digits(x,2).zfill(8)


00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
 
T

te509

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
 
S

Steven D'Aprano

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.
 
M

mensanator

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
 
D

Dennis Lee Bieber

"write a function that takes a sequence of bits as an input and return how

Input from where? Some user typing 0 and 1 on a keyboard -- in which
case your input is a string of characters and ...
many 1s are in the sequence", nothing more. This is quite simple for string

.... this becomes trivial... So trivial I'm going to include a solution,
even though it is a homework assignment...
.... [c for c in inp if c == "1"] )
The count is 12
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

As others have mentioned, numeric literals that begin with 0 are
considered to be in OCTAL (0..7), and those beginning with 0x are
HEXADECIMAL (0..9A..F). You can not specify a BINARY (0..1) literal in
Python.

If the input comes from a user typing, you have a string
representation, and need to know if it is decimal, octal, or hex in
order to convert it to a numeric (integer) object.
Traceback (most recent call last):
Traceback (most recent call last):
Note that the last will convert a string of 0/1 as a binary
representation into the integer equivalent. But for your assignment, as
I show at the top, for a STRING of 0/1, the counting process is trivial
and you don't want to convert it to integer.

Given an integer as the input (not a representation of an integer),
to obtain a count of 1 bits, I'd use a formatting operation to render a
hexadecimal string representation of the integer, then, using a
dictionary of "0".."9""A".."F" mapped to the number of 1 bits in each
character, perform a list comprehension over the string representation,
obtaining a list of counts for each 4-bit hex character; then sum the
elements of the list.

This is also fairly trivial, but as I suspect it may be closer to
the assignment, I will NOT demonstrate actual code...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Steven D'Aprano

... this becomes trivial... So trivial I'm going to include a solution,
even though it is a homework assignment...
... [c for c in inp if c == "1"] )
The count is 12


*cough*

It's even more trivial than that.
12
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top