def __getitem__(self, idx):
        """Extracts a given bit or a range of bits, with python indexing
        semantics.  Use ``extract`` to extract by position and width.

        Since BinInt is, conceptually, an infinite sequence of bits,
        it is an error to use negative indices.  However, it is fine
        to omit the stop index, returning an infinite subsequence
        of bits -- a BigInt is returned in this case.  For finite
        sequences, a BinWord is returned.
        """
        if not isinstance(idx, slice):
            return self.extract(idx, 1)
        step = 1 if idx.step is None else operator.index(idx.step)
        if step == 0:
            raise ValueError('step cannot be 0')
        if step < 0 and idx.start is None:
            raise ValueError('start cannot be None for reverse slicing')
        start = 0 if idx.start is None else operator.index(idx.start)
        stop = None if idx.stop is None else operator.index(idx.stop)
        if start < 0:
            raise ValueError(
                'indexing from the end doesn\'t make sense for a BinInt')
        if stop is not None and stop < 0:
            raise ValueError(
                'indexing from the end doesn\'t make sense for a BinInt')
        if stop is None and step > 0:
            if step == 1:
                return self >> start
            bits = self.bit_length()
            r = range(start, bits, step)
            val = 0
            for opos, ipos in enumerate(r):
                val |= (self >> ipos & 1) << opos
            if self < 0:
                val |= -1 << len(r)
            return BinInt(val)
        else:
            if stop is None:
                stop = -1
            if step == 1:
                if stop <= start:
                    return BinWord(0, 0)
                return self.extract(start, stop - start)
            r = range(start, stop, step)
            val = 0
            for opos, ipos in enumerate(r):
                val |= (self >> ipos & 1) << opos
            return BinWord(len(r), val)
def __getitem__ ( self , idx ) : if not isinstance ( idx , slice ) : return self . extract ( idx , 1 ) step = 1 if idx . step is None else operator . index ( idx . step ) if step == 0 : raise ValueError ( 'step cannot be 0' ) if step < 0 and idx . start is None : raise ValueError ( 'start cannot be None for reverse slicing' ) start = 0 if idx . start is None else operator . index ( idx . start ) stop = None if idx . stop is None else operator . index ( idx . stop ) if start < 0 : raise ValueError ( 'indexing from the end doesn\'t make sense for a BinInt' ) if stop is not None and stop < 0 : raise ValueError ( 'indexing from the end doesn\'t make sense for a BinInt' ) if stop is None and step > 0 : if step == 1 : return self >> start bits = self . bit_length ( ) r = range ( start , bits , step ) val = 0 for opos , ipos in enumerate ( r ) : val |= ( self >> ipos & 1 ) << opos if self < 0 : val |= - 1 << len ( r ) return BinInt ( val ) else : if stop is None : stop = - 1 if step == 1 : if stop <= start : return BinWord ( 0 , 0 ) return self . extract ( start , stop - start ) r = range ( start , stop , step ) val = 0 for opos , ipos in enumerate ( r ) : val |= ( self >> ipos & 1 ) << opos return BinWord ( len ( r ) , val )
Extracts a given bit or a range of bits, with python indexing
        semantics.  Use ``extract`` to extract by position and width.

        Since BinInt is, conceptually, an infinite sequence of bits,
        it is an error to use negative indices.  However, it is fine
        to omit the stop index, returning an infinite subsequence
        of bits -- a BigInt is returned in this case.  For finite
        sequences, a BinWord is returned.