Doing math with unsigned 32bit
BlitzMax Forums/BlitzMax Programming/Doing math with unsigned 32bit
| ||
I have an Int Ptr which is pointing to some 32-bit image data in memory, which holds an unsigned 32-bit number (not rgb, just 32-bit components). Normally Int's are signed. I want to read this unsigned number in and do math with the correct unsigned value. If I do this: Local MyIntPtr:Int Ptr=BasePtr+Xpos+(Ypos*PixelsPerRow) Local a:Long=Long(MyIntPtr[0]) and if the value being read is more than 2^31-1, surely it will read it as some negative number, and then when casting it as a Long it will just make it a long version of that negative number, rather than preserve the unsigned value? How can I read the Int, and only the Int (without having to read a Long from memory), and get the unsigned value into a Long? |
| ||
I guess I answered it myself: With this: Local a:Int=$FEDCBA98 Local b:Int Ptr=Int Ptr(Varptr(a)) Local c:Long=Long(b[0]) Print "Was $FEDCBA98" Print "Is "+Hex$(c Shr 32)+Hex$(c & $FFFFFFFF) produces: Was $FEDCBA98 Is 00000000FEDCBA98 So the answer is, Long() doesn't INTERPRET the value, it just converts it from a 32-bit variable into a 64-bit variable and copies over the BITS, not the interpreted value of the bit. Which is good ;-) |