473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Dec 15 '07 #1
8 3662
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

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.u k <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=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
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.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
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("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
Dec 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2511
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 unsigned char* buffer, size_t start_pos, size_t length) { char value = 0;
15
34576
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
3564
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
3671
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 into a struct timeval object. I get the ByteBuffer as an array of const unsigned char, 'buffer'. Here's an example: 00 00 01 0A 29 1D 07 E4 ...
7
4288
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 fields to the left of it should be made 1. I tried using left shift n right shift operators but that didnt help...any other suggesstions pls?
7
28003
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 bit rgb data. Thanks!!
8
5625
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 ideas of how to approach the problem (1) reading file into unsigned char buffer, 2) defining bit structure, 3) comparing the first 4 bits of the...
2
2245
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. I found that my signal rarely exceeds 1024. So I can considerably reduce the movie size if I store the data in 10 bit and don't allocate all 14...
8
2337
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
19397
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 used as the sign bit for the 16 bit signed integer. Any ideas/help greatly appreciated. Thanks.
0
7824
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8176
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7931
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5370
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.