Skip to content

integers: add support for bit_length() and bit_set() #11618

Description

@massimosala

On Python, from version 2.8, int has the method bit_length

While developing on different versions of Python, I use this:

def bit_length(x):
	if hasattr(x, "bit_length") :
		return	x.bit_length()
	i = 0
	while x :
		x = x >> 1
		i += 1
	return	i

assert	bit_length(0) == 0
assert	bit_length(1) == 1
assert	bit_length((1 << 64) - 1) == 64

Python doesn't have a built-in bit_set, but it is very useful:

def bit_set(x) :
	i = 0
	while x :
		x = x & (x - 1)
		i += 1
	return	i

assert	bit_set(0) == 0
assert	bit_set(1) == 1
assert	bit_set(1 << 64) == 1

The two algorithms are efficient, simple and straightforward to code in C.

Could you please implement them on the int type ?

Activity

  1. robert-hh commented on May 25, 2023

    @robert-hh
    Contributor

    I see bit_length() and bit_count() in the Python documentation, but not bit_set().

  2. mattytrentini commented on May 25, 2023

    @mattytrentini
    SponsorContributor

    bit_set will certainly not be implemented since - as you've mentioned - it's non-standard.

    bit_length could be implemented, however, it's very easy to work-around (with a python example even given in the documentation).

    In general (and also applying to the other issues you've raised), MicroPython has to balance size of implementation with usefulness and convenience. If you really feel strongly that bit_length is necessary, I suggest raising a PR with an implementation (as you've said, it's quite simple) and we can see how large the implementation is and decide whether it's worthwhile.

  3. massimosala commented on May 25, 2023

    @massimosala
    ContributorAuthor

    Hi @mattytrentini

    I know bit_set isn't standard.

    They are two useful functions, working close to hardware.

    IMHO the python example
    https://docs.python.org/3/library/stdtypes.html#int.bit_length
    is an exampe of terrible programming.

    They are usable on Python running on desktop pc, useless on micro for bit-banging and the like.

    Please, is there a How to on PR?
    Even better, an example of coding a basic function?

    Do I need special privileges to open a PR?

    Thanks

  4. robert-hh commented on May 25, 2023

    @robert-hh
    Contributor

    Do I need special privileges to open a PR?

    No.

    Please, is there a How to on PR?

    It's a github thing. See https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request

  5. robert-hh commented on May 25, 2023

    @robert-hh
    Contributor

    Python doesn't have a built-in bit_set,

    Your code example seems to perform the bit_count() operation.

  6. massimosala commented on May 25, 2023

    @massimosala
    ContributorAuthor

    we can see how large the implementation is

    BTW, I think the implemetation will be tiny.
    It isn't rocket science, just basic functions :-)

    I'm willing to try to code the two functions, I've been developing in C (and other languages, on servers and on micros) for 30 years.
    I need a little help getting started.

  7. rkompass commented on May 25, 2023

    @rkompass

    Iirc for bit_length there already exists an old pull request.

  8. robert-hh commented on May 25, 2023

    @robert-hh
    Contributor

    Iirc for bit_length there already exists an old pull request.

    Sure? Do you know the PR number? A search does not reveal it.

  9. dlech commented on May 25, 2023

    @dlech
    SponsorContributor

    It looks like it is attached to this issue, but no PR was made: #4065

  10. rkompass commented on May 25, 2023

    @rkompass

    I meant that.

  11. massimosala commented on May 30, 2023

    @massimosala
    ContributorAuthor

    It looks like it is attached to this issue, but no PR was made: #4065

    Thanks David, I'll close this and continue the discussion on #4065.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementFeature requests, new feature implementations

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions