views:

1313

answers:

2

A C-program is placing what it considers to be 64-bit unsigned integers into a column in a Postgres database that is typed as int8. To Postgres, int8 is always 'signed int8' (no such thing to it as 'unsigned int8'). So the Ruby program I have shows numbers retrieved from Postgres in the upper half of that space as negative.

What is the correct way, in Ruby, to take that -ve integer and convert it to the 64-bit unsigned integer that the C-programmer intended?

+2  A: 

I'm not sure of Ruby specifics, but essentially you need to add 2^64 if the number is negative. That's assuming the number is stored as 2's complement, which it almost certainly is.

FYI, the 2's complement system, essentially it treats a (say) 32-bit number as a number mod 2^32. This means -1 is the same thing as 2^32 - 1 or 0xFFFFFFFF. This turns out to be really simple to use at a hardware level.

Artelius
+2  A: 

This might work:

x += 0x1_0000_0000_0000_0000 if x < 0

The huge constant is 2 to the 64th, as a normal literal. I added the underscores to make it easier to verify that the number is indeed a one followed by 64 zeroes. Each hex digit represents four bits, as always. The underscores are standard Ruby syntax.

unwind