You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
integers: add support for bit_length() and bit_set() #11618
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.
bit_set will 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_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.
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.
Activity
robert-hh commentedon May 25, 2023
I see bit_length() and bit_count() in the Python documentation, but not bit_set().
mattytrentini commentedon May 25, 2023
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.massimosala commentedon May 25, 2023
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
robert-hh commentedon May 25, 2023
No.
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
robert-hh commentedon May 25, 2023
Your code example seems to perform the
bit_count()operation.massimosala commentedon May 25, 2023
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.
rkompass commentedon May 25, 2023
Iirc for
bit_lengththere already exists an old pull request.robert-hh commentedon May 25, 2023
Sure? Do you know the PR number? A search does not reveal it.
dlech commentedon May 25, 2023
It looks like it is attached to this issue, but no PR was made: #4065
rkompass commentedon May 25, 2023
I meant that.
massimosala commentedon May 30, 2023
Thanks David, I'll close this and continue the discussion on #4065.