Bitwise operations in Python?

Discussion in 'Python' started by Carl, Aug 18, 2005.

  1. Carl

    Carl Guest

    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.
    Carl, Aug 18, 2005
    #1
    1. Advertising

  2. Carl

    Paul Rubin Guest

    Carl <> writes:
    > 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.


    >>> def ibits(i,pos,len):

    return (i >> pos) & ~(-1 << len)

    >>> ibits(14,1,3)

    7
    Paul Rubin, Aug 18, 2005
    #2
    1. Advertising

  3. Carl

    Carl Guest

    Incredible, Paul! Thanks a thousand times! /Carl
    Carl, Aug 18, 2005
    #3
  4. Carl

    John Machin Guest

    Carl wrote:
    > 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.
    >


    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(0x12345678, pos, 4, 8-pos/4)
    for pos in range(0, 32, 4):
    testit(0x87654321, pos, 4, 1+pos/4)
    John Machin, Aug 19, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9u?=

    BitWise Operations

    =?Utf-8?B?Sm9u?=, Jan 23, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,349
    =?Utf-8?B?Sm9u?=
    Jan 24, 2006
  2. E. Robert Tisdale

    Re: seeking bitwise operations solution

    E. Robert Tisdale, Aug 17, 2003, in forum: C Programming
    Replies:
    4
    Views:
    436
  3. Chris
    Replies:
    6
    Views:
    2,356
    Daniel Pitts
    Jan 5, 2007
  4. Alan Holloway

    newbie seeks insight on bitwise operations..

    Alan Holloway, Jul 28, 2004, in forum: C Programming
    Replies:
    11
    Views:
    428
    Keith Thompson
    Jul 30, 2004
  5. Replies:
    6
    Views:
    576
    Victor Bazarov
    Apr 19, 2006
Loading...

Share This Page