Repository navigation
integers: add support for bit_length() and bit_set() #11618
Description
Activity
- addedenhancementFeature requests, new feature implementationsFeature requests, new feature implementations
on May 25, 2023 I see bit_length() and bit_count() in the Python documentation, but not bit_set().
bit_setwill certainly not be implemented since - as you've mentioned - it's non-standard.bit_lengthcould 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_lengthis 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.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
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
Reacted by massimosalaPython doesn't have a built-in bit_set,
Your code example seems to perform the
bit_count()operation.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.Iirc for
bit_lengththere already exists an old pull request.Iirc for bit_length there already exists an old pull request.
Sure? Do you know the PR number? A search does not reveal it.
It looks like it is attached to this issue, but no PR was made: #4065
Reacted by massimosalaI meant that.
On Python, from version 2.8, int has the method bit_length
While developing on different versions of Python, I use this:
Python doesn't have a built-in bit_set, but it is very useful:
The two algorithms are efficient, simple and straightforward to code in C.
Could you please implement them on the int type ?