473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

>>>>>>>>>>>>

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.

Aug 18 '05 #1
3 2145
Carl <ph***********@chello.se> 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
Aug 18 '05 #2
Incredible, Paul! Thanks a thousand times! /Carl
Aug 18 '05 #3
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)
Aug 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
4
by: skorobogatov | last post by:
I can't understand a little thing in bitwise operations. In PHP and Javascript 4653896912>>13 = -212992 In Python and Ruby 4653896912>>13 = 568102 In Python and Ruby - it's ok. I...
5
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
11
by: Jordan | last post by:
I am trying to rewrite some C source code for a poker hand evaluator in Python. Putting aside all of the comments such as just using the C code, or using SWIG, etc. I have been having problems...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.