Snippet Useful bit functions

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,345
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:
Max i ask what do you use this stuff for?
 
I'm working on some network code and a hex-editor for a game and my vector-motion editor.
 
Back
Top Bottom