Re: Re: How to locate the bit in bits string?

2009-04-29 Thread Dave Angel
casevh wrote: On Apr 28, 5:39 pm, Li Wang wrote: 2009/4/29 Tim Chase : I want to concatenate two bits string together: say we have '1001' and '111' which are represented in integer. I want to concatenate them to '100' (also in integer form), my method is: ('1001' << 3) | 111 which

Re: How to locate the bit in bits string?

2009-04-29 Thread casevh
On Apr 28, 5:39 pm, Li Wang wrote: > 2009/4/29 Tim Chase : > > >> I want to concatenate two bits string together: say we have '1001' and > >> '111' which are represented in integer. I want to concatenate them to > >> '100' (also in integer form), my method is: > >> ('1001' << 3) | 111 > >> whi

Re: How to locate the bit in bits string?

2009-04-28 Thread Steven D'Aprano
On Wed, 29 Apr 2009 00:36:56 +1000, Li Wang wrote: > Although bin(99)[4] could be used to locate it, this transform cost too > much memory (99 only needs 2Bytes, while string '1100011' needs 7Bytes). This is Python, not C. Your figures are completely wrong. >>> sys.getsizeof(99) 12 >>> sys.getsi

Re: How to locate the bit in bits string?

2009-04-28 Thread Li Wang
2009/4/29 Tim Chase : >>> You omit some key details -- namely how do you know that >>> "1001" is 4 bits and not "1001" (8-bits)? If it's a >>> string (as your current code shows), you can determine the >>> length. However, if they are actually ints, your code should work fine & >>> be O(1). >

Re: How to locate the bit in bits string?

2009-04-28 Thread Tim Chase
You omit some key details -- namely how do you know that "1001" is 4 bits and not "1001" (8-bits)? If it's a string (as your current code shows), you can determine the length. However, if they are actually ints, your code should work fine & be O(1). Actually, what I have is a list of inte

Re: How to locate the bit in bits string?

2009-04-28 Thread Li Wang
2009/4/29 Tim Chase : >> I want to concatenate two bits string together: say we have '1001' and >> '111' which are represented in integer. I want to concatenate them to >> '100' (also in integer form), my method is: >> ('1001' << 3) | 111 >> which is very time consuming. > > You omit some key d

Re: How to locate the bit in bits string?

2009-04-28 Thread David Smith
Li Wang wrote: > 2009/4/29 Tim Chase : >> Li Wang wrote: >>> Hi: >>> >>> If I use an integer to represent bits: >>> e.g. 99 represents '1100011' >>> >>> How can I locate, say the second bit of 99(i.e. '1')? >>> >>> Although bin(99)[4] could be used to locate it, this transform cost >>> too much mem

Re: How to locate the bit in bits string?

2009-04-28 Thread Tim Chase
I want to concatenate two bits string together: say we have '1001' and '111' which are represented in integer. I want to concatenate them to '100' (also in integer form), my method is: ('1001' << 3) | 111 which is very time consuming. You omit some key details -- namely how do you know that

Re: How to locate the bit in bits string?

2009-04-28 Thread Tim Chase
data = file('source.bin').read() def get_bit(source, bit): idx, bit = divmod(bit, 8) byte = ord(source[len(source) - (1+idx)]) return (byte >> bit) & 1 My understanding is: when doing this step, every bit in the byte will be shifted bit-long. If it is get_bit(data, 100), and the sourc

Re: How to locate the bit in bits string?

2009-04-28 Thread Tim Chase
Li Wang wrote: 2009/4/29 Tim Chase : Li Wang wrote: If I use an integer to represent bits: [snip] Hummm, I have tried this method too, the problem is its time complexity. If the length of my bits is n, then the time complexity is O(n). When I tried to implement this in practice, it did consum

Re: How to locate the bit in bits string?

2009-04-28 Thread tuxagb
On 28 Apr, 17:24, Li Wang wrote: > 2009/4/29 Tim Chase : > > > Li Wang wrote: > > >> Hi: > > >> If I use an integer to represent bits: > >> e.g. 99 represents '1100011' > > >> How can I locate, say the second bit of 99(i.e. '1')? > > >> Although bin(99)[4] could be used to locate it, this transfor

Re: How to locate the bit in bits string?

2009-04-28 Thread Li Wang
2009/4/29 Tim Chase : > Li Wang wrote: >> >> Hi: >> >> If I use an integer to represent bits: >> e.g. 99 represents '1100011' >> >> How can I locate, say the second bit of 99(i.e. '1')? >> >> Although bin(99)[4] could be used to locate it, this transform cost >> too much memory (99 only needs 2Byte

Re: How to locate the bit in bits string?

2009-04-28 Thread tuxagb
On 28 Apr, 16:36, Li Wang wrote: > Hi: > > If I use an integer to represent bits: > e.g. 99 represents '1100011' > > How can I locate, say the second bit of 99(i.e. '1')? > > Although bin(99)[4] could be used to locate it, this transform cost > too much memory (99 only needs 2Bytes, while string '

Re: How to locate the bit in bits string?

2009-04-28 Thread Tim Chase
Li Wang wrote: Hi: If I use an integer to represent bits: e.g. 99 represents '1100011' How can I locate, say the second bit of 99(i.e. '1')? Although bin(99)[4] could be used to locate it, this transform cost too much memory (99 only needs 2Bytes, while string '1100011' needs 7Bytes). Anyone

How to locate the bit in bits string?

2009-04-28 Thread Li Wang
Hi: If I use an integer to represent bits: e.g. 99 represents '1100011' How can I locate, say the second bit of 99(i.e. '1')? Although bin(99)[4] could be used to locate it, this transform cost too much memory (99 only needs 2Bytes, while string '1100011' needs 7Bytes). Anyone knows how to loca