473,231 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 3622
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sam Smith | last post by:
Hi, I wan't a function to take a const char*, a start bit position and number of bits and convert that bit-stream into a primitive of desired type. I.e. something like: char convert(const...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
4
by: QQ | last post by:
Hello unsigned char a; the a represents a 4-bit binary, a represents another 3-bit binary. I'd like to convert a into a decimal. For instance if {a a a a the decimal should be 5
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
7
by: farnaz.shahed | last post by:
Hi, This what I'm basically supposed to do. If i have a byte which looks like this 00010000, i need to convert it to 11110000. That would mean if any of the bit fields is 1....all the bit...
7
by: anuragkhanna8 | last post by:
Hi, I am trying to convert a 16 bit rgb value to 32 bit, however the new color generated is different from the 16 bit rgb data. Please let me know the formula to convert an 16 bit rgb data to 32...
8
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some...
2
by: runcyclexcski | last post by:
Hi all, I don't have basic training in programming, so I apologize for this naiive question. I have a camera that outputs 14 bit data. I need to collect and store movies of thousands of frames....
8
by: Hahnemann | last post by:
Is there a sample in C out there to represent a sequence of numbers (shorts) using bits? With the intention of saving space, instead of using the full 16 bits per short. Thanks. - Hahnemann
28
by: Fore | last post by:
Hello I am looking for some effecient way to convert a 32 bit unsigned integer to a 16 bit signed integer. All I want is the lower 16 bits of the 32 bit unsigned integer , with bit 15 (0..15) to...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.