Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DWORD multiplication in python

Tags:

python

I have a problem, for which an answer I've been looking for too long.

In one application, written in PowerBuilder, there has been a bug (which has not been spotted on time), which went as follows - long * long results in long (not in longlong), due to what the output was somehow strange. Now I need to do the same in python. For example:

a = 3423729
b = 96300
c = a*b = 329705102700

PowerBuilder says:

c = a*b = -1007379092

Due to further tests, it seems like it is a signed 32-bit integer, but I couldn't make python return me such result. Anyways, using calc and making him go with dword multiplication, result is -1007379092. Any clue how to resolve that matter?

like image 700
Mariusz Mizgier Avatar asked May 19 '26 13:05

Mariusz Mizgier


2 Answers

You can use the ctypes package:

>>> from ctypes import c_int32
>>> a = 3423729
>>> b = 96300
>>> c_int32(a*b).value
-1007379092
like image 182
Ferruccio Avatar answered May 22 '26 02:05

Ferruccio


It seems that your application is taking the rightmost 32 bits of the result, represented as a 32 bit int.

You can use the types in the ctypes library to represent signed/unsigned integers of a specific size, in a similar manner. For a signed 32 bit int:

>>> import ctypes
>>> c = 329705102700
>>> ctypes.c_int32(c).value
-1007379092
like image 45
interjay Avatar answered May 22 '26 03:05

interjay