How to locate the bit in bits string?

L

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 locate the second bit without using bin() function?

Thank you very much:D
 
T

tuxagb

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 locate  the second bit without using bin() function?

Thank you very much:D

If we consider 8 bit, a solution may be the follow:
99

as you view, you set the nth bit to 0, and view if the result is same
as 'a'. If is same
then the nth bit doesn't set. If you write the above code in a loop,
you can test which bit
you want.

Hi.
 
S

Steven D'Aprano

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


Everything in Python is an object. If you're expecting C performance for
bit-twiddling operations, you aren't going to get it.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top