Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Bitwise operations in Python?

Reply
Thread Tools

Bitwise operations in Python?

 
 
Carl
Guest
Posts: n/a
 
      08-18-2005
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.

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      08-18-2005
Carl <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Carl
Guest
Posts: n/a
 
      08-18-2005
Incredible, Paul! Thanks a thousand times! /Carl
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      08-19-2005
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bitwise operations on a byte array Chris Java 6 01-05-2007 05:51 PM
floating point bitwise & and >> operations jawilson2@gmail.com C++ 6 04-19-2006 04:00 AM
BitWise Operations =?Utf-8?B?Sm9u?= ASP .Net 3 01-24-2006 09:19 AM
newbie seeks insight on bitwise operations.. Alan Holloway C Programming 11 07-30-2004 09:51 PM
Re: seeking bitwise operations solution E. Robert Tisdale C Programming 4 08-18-2003 02:15 AM



Advertisments