• 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 Useful bit functions

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,414
This adds some routines that handle bits in an exact and easy way.

Cerberus:
Import mojo2

Function Main()
    New Game
    Return 0
End

Class Game Extends App

    Method OnCreate()

        Local value:Int = 65535  
        value = Shr2(value,8)        ' Logic shift right (logical shift left is the same as non-logical Shl) the possible shift-amount is 0-32, not limitted to 31      
        Print "Decimal:"
        Print value
        Print "The number of bits it contains:"
        Print CountBits(value)         ' Count the set bits.
        Print "Binary representation of all the bits:"
        Print BinaryString(value)    ' Write the bits.  
        Print "Hexadecimal representation:"
        Print Dec2Hex(value)         ' "ff"
        Print "Will now print the correct 32-bits and deal with the sign-bit correctly:"
        Print Dec2Hex($ffffffff) ' Should write "ffffffff"

        Return 0
    End

End

Function Overflow:Int(a:Int,b:Int)
    Local result:Int = 0
    result = a + b
    If(a > 0 And b > 0 And result < 0) Then Return -1
    If(a < 0 And b < 0 And result > 0) Then Return -1
    Return 0
End

Function Shr2:Int(x:Int,y:Int) ' The only missing shift instruction (>>> in JS and C++).
    If y = 0 Then Return x
    Local mask:Int = $7FFFFFFF
    mask = mask Shr (y-1)
    x = (x Shr y) & mask
    Return x
End

Function Trunc:Int(number:Float)
    If number > 0 Then Return Floor(number) Else Return Ceil(number)
End

Function Len:Int(s:String)
    Return s.Length
End

Function Mid:String(s:String,p:Int,n:Int)
    p=p-1
    Return s[(p)..(p+n)]
End

Function Left:String(s:String,n:Int)
    Return s[0..n]
End

Function Right:String(s:String,n:Int)
    Return s[(s.Length()-n)..(s.Length())]
End

Function Chr:String(n:Int)
    Return FromChar(n)
End

Function Val:Int[](s:String)
    Return ToChars(s)[0]
End

Function CountBits:Int(x:Int)
    Local n:Int = x
    Local count:Int = 0
    Local mask:Int = 1
    For Local temp:Int = 0 To 31
    If ( (mask & x) = mask) Then count = count + 1
    mask = mask Shl 1
    Next
    Return count
End

Function BinaryString:String(x:Int)
    Local all:String = ""
    For Local temp:Int = 31 To 0 Step -1
        If (x Shr temp) & 1 Then all=all+"1" Else all=all+"0"
        If temp Mod 8 = 0 Then all=all+" "
    Next
    Return all
End

' Another example how to make binary print
' Function Dec2Bin:String(value:Int)
'     Local base:Int = 2
'     Local m:String=""
'     Repeat
'         Local rem:Int = value Mod base
'         value =  Shr2(value,1)
'         m = Mid("0123456789abcdefghijklmnopqrstuvwxyz",rem + 1,1) + m      
'     Until value = 0
'     Return m
' End

Function Dec2Hex:String(value:Int) ' No general base for now
    Local base:Int = 16
    Local m:String=""
    Repeat
        Local rem:Int = value Mod base
        value =  Shr2(value,4) ' This makes it all possible to deal with all 32-bits. Correct handling of the highest signbit.
'        value = value / base ' Only Hex for now as / is problematic with bit 31
        m = Mid("0123456789abcdefghijklmnopqrstuvwxyz",rem + 1,1) + m      
    Until value = 0
    Return m
End
 
Last edited:
I'm working on some network code and a hex-editor for a game and my vector-motion editor.
 
Back
Top Bottom