New submission from anon:

For many numeric algorithms it's useful to be able to read individual bits at a 
location in an integer. Currently there is no efficient way to do this. The 
following function is the closest to this:

def bit_at(i, n): return (i>>n)&1

However in computing the intermediate result i>>n we must spend O(b-n) time at 
least (where b is n.bit_length()). It should be possible to read bits in O(1) 
time. Adding int.bit_at(n) would complement int.bit_length().

I would suggest making the semantics of i.bit_at(n) the same as (i>>n)&1. 
Although the exact meaning when i is negative should be considered.

Real world uses of bit_at include binary exponentiation and bit counting 
algorithms.

----------
messages: 205421
nosy: anon
priority: normal
severity: normal
status: open
title: int.bit_at(n) - Accessing a single bit in O(1)
type: enhancement
versions: Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19915>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to