Game Development Community

Extending integer length

by Eero Karvonen · in Torque 2D Beginner · 11/20/2013 (1:25 pm) · 6 replies

Hi,

I spent a lot of hours trying to figure out what was wrong with my bitwise operation. It turned out nothing was wrong, but the integer data had been messed up when Torque read it from an XML document.

The thing is, my numbers fill up all the 32 bits. However, if the highest bit is set, it seems to be ignored, and an overflow occurs.

What is the most common way to overcome this issue? Do I need to modify and recompile some parts of the engine, and is there any straightforward, guided way to do this?

Thanks!

#1
11/20/2013 (2:03 pm)
Use 64 bit integers - or perhaps use a bitset.

Signed: From -2,147,483,648 to 2,147,483,647
Unsigned: From 0 to 4,294,967,295

That's just the way it is.

The bitset is probably your best bet - it's not interpreted as a "number" exactly - just as a collection of flags.
#2
11/22/2013 (12:35 pm)
Just to make sure I'm looking for this in the right domain: is there a way to define 64-bit integers in TS? My current understanding is that there are only signed 32-bit numeral variables and strings. What should I do instead of the following line?
%value = %XMLdocument.attributeS32 ("attribute");
#3
11/23/2013 (1:55 am)
Use a bitset. Google has many many hits on bitsets in c++. How you stuff it into a XML doc is a matter of taste and determination, I guess....
#4
11/27/2013 (8:24 am)
Thanks, Richard! I'm sure it would work, but due to my insufficient skills at C++, I'd rather try to use a premodifier patch than to fiddle with the engine myself.

But let's assume I managed to do this in C++. In that case would the following be a viable way?

Writing a Console Method which takes a string representing the 64-bit value in question, then converts it into a U64 (or a bitset), does the needed boolean operations to remove the flag-bits (due to whom I have this big variables in the first place...). The Method would then return a S32 to be compatible with the rest of the engine.
#5
11/29/2013 (12:27 am)
I'm missing something - Usually when you're down to twiddling bits it is because you're storing a set of on/off (binary) flags. If you're looking to have it represent a meaningful number you might have to do something else.
#6
11/29/2013 (12:00 pm)
I'm reading a format, which has integer values stored as 32-bit positive numbers. These numbers, however, are stored as "hybrids" of actual number data and flags: Only the first 29 bits represent the number (U29, if you will) and the rest three are flags.

What I need to be able to do, is to read those 32-bit values from a file (they are actually strings representing numbers), read the flags and then set them as zeros, and return the numeric data as positive 32-bit numbers. But at the conversion from a string-->S32 the engine gives me an overflow effect: If the positive value is too big, the data is lost, of course.

One, albeit highly inefficient way to circumvent the problem would probably be to create a function which reads the string-number and finds its negative S32 counterpart on a high level. So the bits would at least be intact from conversion.