Signed short

Started by Flanker, November 19, 2017, 01:55:26

Previous topic - Next topic

Flanker

Hello,
is there a way to use signed short in Blitzmax or NG ? So far i'm using integers for some heavy calculation, but I noticed a huge speed improvment using short instead, the thing is I need negative numbers... Stuff like conversion to integer or extra calculation would kill the speed bonus.

Maybe a library for blitzmax ? Or NG has it built-in ?
Everyone knew it was impossible, until someone who didn't know made it.

RonTek

Hey Flanker, this could be related, but not sure if this is exactly what you are looking for..

https://www.syntaxbomb.com/index.php?topic=1812.0

added this to BC archives as well:

http://blitzcoder.org/forum/codearcs.php?category=327

TomToad

#2
because of the way that twos compliment works, you can use shorts as though they are signed.  Only problem is when you use the final result, in which case, you need to convert to Int and check if bit 15 is set.  If so, then convert the twos compliment to a signed Int.

Code (blitzmax) Select
SuperStrict
Local a:Short = -10 'assign the number to the short.  Don'y worry
Local b:Short = 3   '  about the negative number.  It will automatically
                    '  be converted using twos compliment.

Local c:Int = a+b/3 'Do your math.  Store the final result as an int so
                    '  it can be printed correctly later

If c >= $8000 Then c = -((~c&$FFFF)+1)  'Check if bit 15 is set.  If so
                                        '  Then convert to a signed Int

Print c

------------------------------------------------
8 rabbits equals 1 rabbyte.

Flanker

Thanks for the answers. I'll have to test that in depth to see if it worth using shorts.
Everyone knew it was impossible, until someone who didn't know made it.