Shifts

Jimmy

Active member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
919
Either I'm heavily confused or this is really worth noting ;

When it comes to some features such as unsigned datatypes and suitable operators, Cerberus has been forced to follow along to the major programming languages it uses underneath.

For instance, C implements only two shifts, C++ implements all three shifts, Objective C implements two shifts as C *but* the right one can be either arithmetic or logic depending on platform, and you cannot change or have any control over which one it'll be. JAVA used to not have unsigned datatypes so that makes you wonder why you should even care to differentiate between signed and not signed shifts. For just a few months ago both JAVA and Kotlin got unsigned and it's just about to get mature..

Anyhow, In Cerberus I always thought the shifts were logical, I've been so sure about this so I was eventually today about to implement an ARITHMETICAL shift in code just to try something out, and to my surprise, it did give the correct answer *without* it. It seems like Cerberus actually has *only* arithmetic shifts?

EDIT So change the amount to 4 and you'd get -1 instead of -3.. I don't know what's going on here to be honest.. will need to investigate.

Code:
Strict

Function Main:Int()

    Local x:Int = 10

       Local amount:Int = 1
     x = -12
     Print x

     Print "--------------"
     'If x < 0 Then x = ~(~x Shr amount) Else x = x Shr amount ' Simulate Sar (arithmetic shift right)
      x = x Shr amount ' Logic shift right (should not work with signed)
     Print x

    Return 0    
End
 
Last edited:

Jimmy

Active member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
919
As HTML5 has all the shifts and datatypes in the world I made something I could use as reference while testing.
Maybe someone else has use for it too..

test.cxs
Code:
Strict

Import "commands.js"
Extern
Function left:Int(n:Int, value:Int)
Function right:Int(n:Int, value:Int)

Public

Function Main:Int()

     Local x:Int = 10

     Local amount:Int = 1
     x = -12
     Print x

     Print "--------------"
     ' If x < 0 Then x = ~(~x Shr amount) Else x = x Shr amount ' Simulate Sar (arithmetic shift right)
     'x = x Shr amount ' Logic shift right (should not work with signed)
     Print x

     Print right(x,16) ' 1 = /2
     Print left(x,16) ' 1 = *2 ' It's intact!

    Return 0      
End
commands.js
Code:
var logic = {
    Right : function(n, value) {
        return n >>> value;
    },

    Left : function(n, value) {
        return n << value;
    }
};
 
Last edited:
Top Bottom