473,379 Members | 1,278 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,379 software developers and data experts.

32 bit arithmetic in 2.3+

I'm sure we've had this discussion before, but I'm getting a bunch of
problems related to various algorithms related to 32 bit arithmetic.

The particular error/warnings I'm seeing are

FutureWarning: hex/oct constants > sys.maxint will return positive
values in Python 2.4 and up

FutureWarning: x<<y losing bits or changing sign will return a long in
Python 2.4 and up

related to the use of 0x81020304 and << respectively.

Can someone tell me

1) What feature makes these warnings errors?

2) What's the right way to do the bit shifting modulo 32 bits?
The same kinds of problems exist in java, but we don't get the warnings
regarding the use of 0x80000000. Do I need to replace all 0x8........
numbers with the L version and ensure all << shifts are masked with
0xffffffffL ? Surely there will be some compatibility issues.
--
Robin Becker
Jul 18 '05 #1
3 3763
On Thu, 23 Oct 2003 11:10:28 +0100, Robin Becker <ro***@jessikat.fsnet.co.uk> wrote:
I'm sure we've had this discussion before, but I'm getting a bunch of
problems related to various algorithms related to 32 bit arithmetic.

The particular error/warnings I'm seeing are

FutureWarning: hex/oct constants > sys.maxint will return positive
values in Python 2.4 and up

FutureWarning: x<<y losing bits or changing sign will return a long in
Python 2.4 and up

related to the use of 0x81020304 and << respectively.

Can someone tell me

1) What feature makes these warnings errors?

2) What's the right way to do the bit shifting modulo 32 bits?
The same kinds of problems exist in java, but we don't get the warnings
regarding the use of 0x80000000. Do I need to replace all 0x8........
numbers with the L version and ensure all << shifts are masked with
0xffffffffL ? Surely there will be some compatibility issues.


I guess the messages mean that your int objects are not the same as what int is
to mean soon. So maybe being explicit might be the way to go? E.g.,

class int32(int):
# ... make it work the way you want... or make a separate int32 extension

and use int32 instances where you have int instances now? It would also document
your 32-bit assumptions/requirements.

What do your 32-bit entities represent? Perhaps there is something even more
appropriate than integers?

If you do need good-old-32-bit-ints modeling typical 32-bit ALU behavior, and a
lot of people need that, maybe there ought to be a built-in int32 type, or a
parameterized fixed-width metatype, so you could say int32 = int_fixed_width(32)
or int_sixty_four = int_fixed_width(64) etc. and tie into efficient implementation.

Just a few rambling thoughts...

Regards,
Bengt Richter
Jul 18 '05 #2
In article <bn**********@216.39.172.122>, Bengt Richter <bo**@oz.net>
writes
..
.......

I guess the messages mean that your int objects are not the same as what int is
to mean soon. So maybe being explicit might be the way to go? E.g.,

class int32(int):
# ... make it work the way you want... or make a separate int32
extension

and use int32 instances where you have int instances now? It would also
document
your 32-bit assumptions/requirements.

What do your 32-bit entities represent? Perhaps there is something even more
appropriate than integers?
I think you're right. These things represent the checksums done in
typical C code where int means 32 bit unsigned. Of course the C code
will go wrong when it moves to a 64 bit machine, but I didn't design
true type fonts or the pdf encryption algorithms.
If you do need good-old-32-bit-ints modeling typical 32-bit ALU behavior, and a
lot of people need that, maybe there ought to be a built-in int32 type, or a
parameterized fixed-width metatype, so you could say int32 =
int_fixed_width(32)
or int_sixty_four = int_fixed_width(64) etc. and tie into efficient
implementation.

This seems like an excellent idea. I suppose it could be done in Python
first to provide a reference implementation.
Just a few rambling thoughts...

Regards,
Bengt Richter


It seems a bit funny to issue Future Warnings about things that I cannot
import a __future__ feature and test any fix.
--
Robin Becker
Jul 18 '05 #3
Hello Robin,
I'm sure we've had this discussion before, but I'm getting a bunch of
problems related to various algorithms related to 32 bit arithmetic.

The particular error/warnings I'm seeing are

FutureWarning: hex/oct constants > sys.maxint will return positive
values in Python 2.4 and up

FutureWarning: x<<y losing bits or changing sign will return a long in
Python 2.4 and up

related to the use of 0x81020304 and << respectively.

Can someone tell me

1) What feature makes these warnings errors? Currently bitwise operation are on the system "native" types.
In 2.4 they will be a "logical" operation since ints and long ints
will be unified.
currently:
1<<64 __main__:1: FutureWarning: x<<y losing bits or changing sign will
return a long in Python 2.4 and up
0 1L<<64 18446744073709551616L

In 2.4 and up 1<<64 will give the same result as 1L<<64
2) What's the right way to do the bit shifting modulo 32 bits?
The same kinds of problems exist in java, but we don't get the warnings
regarding the use of 0x80000000. Do I need to replace all 0x8........
numbers with the L version and ensure all << shifts are masked with
0xffffffffL ? Surely there will be some compatibility issues.

IMO this is the best way.
def lshift32(n):
return (n << 32) & (0xFFFFFFFF)

HTH.
Miki
Jul 18 '05 #4

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

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
3
by: Karthik | last post by:
Hi, I am writing this application that needs a lot of arithmetic calculations. I was wondering if C++ language specifies any way of detecting arithmetic overflows. Let us consider the following...
10
by: Massimiliano Alberti | last post by:
Are there rules on how should the C behave with the arithmetic operations? Are the overflows always ignored? And are they simply truncated? (if I have a 16-bits unsigned short ints unsigned short...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
4
by: PDHB | last post by:
I'm sorry, but this is just the height of stupidity. I love the dot net framework. It has actually been sufficient to convert an anti-microsoft extremist (myself) to the windows camp. But not...
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
27
by: jacob navia | last post by:
As Richard Bos rightly pointed out, I had left in my classification of types the C99 types Complex and boolean. Here is a new classification. Those are not mentioned in the classification of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.