Simulating int arithmetic with wrap-around

2016-12-30 Thread Steve D'Aprano
It's easy to simulate integer arithmetic with wrap-around for *unsigned* ints. For example, imagine a four-bit integer (0 through 15): # Addition py> (7 + 8) & 0xF 15 py> (7 + 9) & 0xF 0 py> (7 + 10) & 0xF 1 # Multiplication py> (7 * 2) & 0xF 14 py> (7 * 3) & 0xF 5 py> (7 * 4) & 0xF 12 And in g

Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Serhiy Storchaka
On 30.12.16 16:47, Steve D'Aprano wrote: Again, assume both operands are in range for an N-bit signed integer. What's a good way to efficiently, or at least not too inefficiently, do the calculations in Python? def to_unsigned(bits, x): return x & ((1

Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Chris Angelico
On Sat, Dec 31, 2016 at 1:47 AM, Steve D'Aprano wrote: > How about *signed* integers? > > 7 + 1 => -8 > 7 + 2 => -7 > 7 + 7 => -2 > > 7 * 2 => -2 > 7 * 3 => 5 > 7 * 4 => -4 > > > Again, assume both operands are in range for an N-bit signed integer. What's > a good way to efficiently, or at least n

Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Steve D'Aprano
On Sat, 31 Dec 2016 02:14 am, Serhiy Storchaka wrote: > On 30.12.16 16:47, Steve D'Aprano wrote: >> Again, assume both operands are in range for an N-bit signed integer. >> What's a good way to efficiently, or at least not too inefficiently, do >> the calculations in Python? > > def to_unsigned(b

Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Serhiy Storchaka
On 31.12.16 02:30, Steve D'Aprano wrote: Are you saying that the best way of doing this is this? (1) convert signed Python ints to unsigned; (2) perform operation and bitmask; (3) convert unsigned back to signed. Here's an example. I want to multiply 7*3 using a signed 4-bit int, getting 5 a

Re: Simulating int arithmetic with wrap-around

2017-01-03 Thread Paul Rubin
Steve D'Aprano writes: > Again, assume both operands are in range for an N-bit signed integer. What's > a good way to efficiently, or at least not too inefficiently, do the > calculations in Python? My first thought is towards the struct module, especially if you want to handle a bunch of such in

Re: Simulating int arithmetic with wrap-around

2017-01-03 Thread Gregory Ewing
Paul Rubin wrote: My first thought is towards the struct module, especially if you want to handle a bunch of such integers at the same time. Or maybe the array module or some combination. Or possibly numpy. -- Greg -- https://mail.python.org/mailman/listinfo/python-list

Re: Simulating int arithmetic with wrap-around

2017-01-04 Thread Random832
On Fri, Dec 30, 2016, at 09:47, Steve D'Aprano wrote: > Again, assume both operands are in range for an N-bit signed integer. > What's > a good way to efficiently, or at least not too inefficiently, do the > calculations in Python? I'd do something like: bit_mask = (1 << bits) - 1 # 0x sign_b

Re: Simulating int arithmetic with wrap-around

2017-01-04 Thread Rob Gaddi
On 01/03/2017 10:00 PM, Gregory Ewing wrote: Paul Rubin wrote: My first thought is towards the struct module, especially if you want to handle a bunch of such integers at the same time. Or maybe the array module or some combination. Or possibly numpy. Agreed. If you had to do a lot of cal

Re: Simulating int arithmetic with wrap-around

2017-01-05 Thread Paul Rubin
Steve D'Aprano writes: > Again, assume both operands are in range for an N-bit signed integer. What's > a good way to efficiently, or at least not too inefficiently, do the > calculations in Python? My first thought is towards the struct module, especially if you want to handle a bunch of such i

Re: Simulating int arithmetic with wrap-around

2017-01-06 Thread Gregory Ewing
Paul Rubin wrote: > My first thought is towards the struct module, especially if you want to > handle a bunch of such integers at the same time. Or maybe the array > module or some combination. Or possibly numpy. -- Greg -- https://mail.python.org/mailman/listinfo/python-list

Re: Simulating int arithmetic with wrap-around

2017-01-06 Thread Random832
On Fri, Dec 30, 2016, at 09:47, Steve D'Aprano wrote: > Again, assume both operands are in range for an N-bit signed integer. > What's > a good way to efficiently, or at least not too inefficiently, do the > calculations in Python? I'd do something like: bit_mask = (1 << bits) - 1 # 0x sign_b

Re: Simulating int arithmetic with wrap-around

2017-01-06 Thread Rob Gaddi
On 01/03/2017 10:00 PM, Gregory Ewing wrote: > Paul Rubin wrote: >> My first thought is towards the struct module, especially if you want to >> handle a bunch of such integers at the same time. Or maybe the array >> module or some combination. > > Or possibly numpy. > Agreed. If you had to do a