• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Snippet How to put both x & y inside one int

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,414
This is a simple example on how to put both x & y inside one int
It also shows you how to create an efficient stack, without security checks.

This way we can shove away many coordinates with great speed and less memory.

Cerberus:
' -------------------------------------------------------------
' Say that we want to push a lot of positions efficiently
' x & y can each be 0-16383 in this example
' -------------------------------------------------------------

Strict

    Global data:Int[65535]           ' Create a stack
    Global sp:Int = 32768            ' Create a stack pointer, prepared for a circular datastack

Function Main:Int()

    Local x:Int = 10 ' Set coordinates
    Local y:Int = 100

    ' PUSH X,Y -------------------------------
    sp = sp - 1 ; data[sp] = ( (x Shl 14) + y)
    ' ------------------------------------------

    x = 435833 ' destroy coordinates
    y = 34894

    ' POP X,Y ---------------------------------------------------------------------
    Local m:Int = data[sp] ; sp = sp + 1 ; x = (m Shr 14) & 16383 ; y = m & 16383
    ' ------------------------------------------------------------------------------

    Print x ' Proove that we have restored the coordinates
    Print y

    Return 0       
End
 
Last edited:
Interesting approach and I can see a use case in data transmission over the internet where sending as less data as possible is important.
 
Why shift left 14 and not 16?

32768 = 2 ^16, not 14. You should do (y shl 16) + x to store two 'small ints' in there - and that will only work if values are>=0 - if you're working with negative values that will break down if you don't use at least 1 bit for the signal.
 
The original 14-bit works 100% reliably.

But going to 15-bit and 16-bit now :
Using the full range must come with a warning. I made lots of tests before and they all point to that the full bit range does not work as you would expect, you'd might as well limit yourself and be safe with the 14-bit version. I would consider everything else unusable.

I did all these tests last week, I re-made a few tests this morning seeing the reaction post.

Note, why 15-bit work as bad as 16-bit which I cannot explain. If I had to guess It might be because the fact that we're dealing with two numbers so we need two ("sign") bits extra space.

Cerberus:
Strict

    Global data:Int[65535]           ' Create a stack
    Global sp:Int = 32768            ' Create a stack pointer, prepared for a circular datastack

Function Main:Int()

    Local x:Int = 10000 ' Set coordinates
    Local y:Int = 100000

    ' Twos complement (same as a xs = -xs)
    Local xs:Int = 0
    xs = x -1
    xs =  xs ~ -1
    Print "----"
    Print xs
    Print "----"

    ' PUSH X,Y -------------------------------
  '  x = x + 65536 ; y = y + 65536
    sp = sp - 1 ; data[sp] = ( (x Shl 16) + y)
    ' ------------------------------------------

    x = 435833 ' destroy coordinates
    y = 34894

    ' POP X,Y ---------------------------------------------------------------------
    Local m:Int = data[sp] ; sp = sp + 1 ; x = (m Shr 16) & 65535 ; y = m & 65535
   ' x = x - 65536 ; y = y - 65536
    ' ------------------------------------------------------------------------------

    Print x
    Print y

    Return 0 
End
 
Last edited:
Back
Top Bottom