Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Integer overflow

by gone2015 (Deacon)
on Apr 25, 2009 at 09:41 UTC ( [id://759972]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Integer overflow
in thread Integer overflow

OK...

{ my $q = $result & 0xFFFF_FFFF ; $result = $q <= 0x7FFF_FFFF ? $q : -1 - ($q ^ 0xFFFF_FFFF) ; } ;
...the objective is to take the LS 32 bits of an integer, 32 bit or more, signed or unsigned, and set $result to be the signed 32 bit integer value. So:

  1. $result & 0xFFFF_FFFF extracts the LS 32 bits and returns an unsigned value.

  2. if the value is now <= 0x7FFF_FFFF, then it's positive, and we're done...

  3. ...otherwise we have 0x8000_0000..0xFFFF_FFFF which we need to map to -0x8000_0000..-0x0000_0001. The XOR $q ^ 0xFFFF_FFFF gives 0x7FFF_FFFF..0x0000_0000, so we complete the process by negating that and subtracting 1... or subtracting that from -1.

    This could also be written -(($q ^ 0xFFFF_FFFF) + 1)or -((~$q + 1) & 0xFFFF_FFFF)... which are closer to the conventional way of changing the sign -- noting that we want this to work for any size of integer from 32 up.

This is assuming 2's complement signed integers. I cannot recall a sign and magnitude machine (for integers). The last 1's complement machine I worked on was a CDC6400 in the 70's. So it's a fairly safe assumption.

But, you could always: unpack('l', pack('L', $result & 0xFFFF_FFFF)), which ensures a valid argument for pack('L', ...) and makes no assumptions about how unpack('l', ...) interprets the bits.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://759972]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found