Bitwise operations in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl

    #1

    Bitwise operations in Python?

    Dear friends,

    I am currently porting a fortran program to Python but am stuck on the
    intrinsic IBITS function.

    Does anyone know about a replacement function for IBITS in Python?

    Yours, Carl

    [color=blue][color=green][color=darkred]
    >>>>>>>>>>>>>>> >[/color][/color][/color]


    IBITS(I, POS, LEN)

    Extracts a sequence of bits.

    I
    must be of type integer.

    POS
    must be of type integer. It must be nonnegative and POS + LEN must be
    less than or equal to BIT_SIZE (I).

    LEN
    must be of type integer and nonnegative.

    Class

    Elemental function

    Result Type and Attributes

    Same as I.

    Result Value

    The result has the value of the sequence of LEN bits in I beginning at bit
    POS, right-adjusted and with all other bits zero.

    The bits are numbered from 0 to BIT_SIZE(I)-1, from right to left.

    Examples

    IBITS (14, 1, 3) has the value 7.

  • Paul Rubin

    #2
    Re: Bitwise operations in Python?

    Carl <phleum_nospam@ chello.se> writes:[color=blue]
    > IBITS(I, POS, LEN)
    > Extracts a sequence of bits.
    > The result has the value of the sequence of LEN bits in I beginning at bit
    > POS, right-adjusted and with all other bits zero.
    >
    > The bits are numbered from 0 to BIT_SIZE(I)-1, from right to left.
    >
    > Examples
    >
    > IBITS (14, 1, 3) has the value 7.[/color]
    [color=blue][color=green][color=darkred]
    >>> def ibits(i,pos,len ):[/color][/color][/color]
    return (i >> pos) & ~(-1 << len)
    [color=blue][color=green][color=darkred]
    >>> ibits(14,1,3)[/color][/color][/color]
    7

    Comment

    • Carl

      #3
      Re: Bitwise operations in Python?

      Incredible, Paul! Thanks a thousand times! /Carl

      Comment

      • John Machin

        #4
        Re: Bitwise operations in Python?

        Carl wrote:[color=blue]
        > Dear friends,
        >
        > I am currently porting a fortran program to Python but am stuck on the
        > intrinsic IBITS function.
        >
        > Does anyone know about a replacement function for IBITS in Python?
        >
        > Yours, Carl
        >
        > IBITS(I, POS, LEN)
        >
        > Extracts a sequence of bits.
        >
        > I
        > must be of type integer.
        >
        > POS
        > must be of type integer. It must be nonnegative and POS + LEN must be
        > less than or equal to BIT_SIZE (I).
        >
        > LEN
        > must be of type integer and nonnegative.
        >
        > Class
        >
        > Elemental function
        >
        > Result Type and Attributes
        >
        > Same as I.
        >
        > Result Value
        >
        > The result has the value of the sequence of LEN bits in I beginning at bit
        > POS, right-adjusted and with all other bits zero.
        >
        > The bits are numbered from 0 to BIT_SIZE(I)-1, from right to left.
        >
        > Examples
        >
        > IBITS (14, 1, 3) has the value 7.
        >[/color]

        No, don't know, but you could write one yourself PDQ. Something like this:

        def ibits(arg, pos, len):
        return (arg >> pos) & ((1 << len) - 1)

        if __name__ == "__main__":

        def testit(arg, pos, len, reqd):
        result = ibits(arg, pos, len)
        print arg, pos, len, reqd, result, "* "[result == reqd]

        testit(14, 1, 3, 7)
        for pos in range(6):
        testit(4095, pos, 3, 7)
        for pos in range(6):
        testit(-1, pos, 3, 7)
        for pos in range(6):
        testit(0, pos, 3, 0)
        for pos in range(0, 32, 4):
        testit(0x123456 78, pos, 4, 8-pos/4)
        for pos in range(0, 32, 4):
        testit(0x876543 21, pos, 4, 1+pos/4)

        Comment

        Working...