B4J Question Long bit or and bit and

Robert Valentino

Well-Known Member
Licensed User
Longtime User
In B4A I do the following to work on Longs bits
B4X:
#Region CreateJO JavaObject
Private Sub CreateJO As JavaObject
     Dim jo As JavaObject
    
     jo.InitializeNewInstance(Application.PackageName & ".cgenfuncs", Null)
    
     Return jo
End Sub
#End Region

#Region LandANDTest / LongAND / LongOR LongNOT
Public  Sub LongANDTest(N1 As Long, N2 As Long)  As Boolean

     If LongAND(N1, N2) <> 0 Then Return True
    
     Return False
End Sub

Public  Sub LongAND(N1 As Long, N2 As Long) As Long
      Dim jo As JavaObject = CreateJO
    
      Return jo.RunMethod("andLong", Array(N1, N2))
End Sub

Public  Sub LongOR(N1 As Long, N2 As Long) As Long
      Dim jo As JavaObject = CreateJO
    
  Return jo.RunMethod("orLong", Array(N1, N2))    
End Sub    

Public  Sub LongShiftLeft(N1 As Long, BitsToShift As Int) As Long
      Dim jo As JavaObject = CreateJO
    
  Return jo.RunMethod("ShiftLeftLong", Array(N1, BitsToShift))    
End Sub

Public  Sub LongShiftRight(N1 As Long, BitsToShift As Int) As Long
      Dim jo As JavaObject = CreateJO
    
  Return jo.RunMethod("ShiftRightLong", Array(N1, BitsToShift))    
End Sub

Public  Sub LongNOT(N1 As Long) As Long
      Dim jo As JavaObject = CreateJO
    
  Return jo.RunMethod("flipBits", Array(N1))    
End Sub

Public  Sub LongAbs(N1 As Long) As Long
      Dim jo As JavaObject = CreateJO
    
  Return jo.RunMethod("nosign", Array(N1))    
End Sub

#If JAVA
public long andLong(long n1, long n2)       { return n1 & n2;     }
public long orLong(long n1, long n2)        { return n1 | n2;     }
public long XorLong(long n1, long n2)       { return n1 ^ n2;     }
public long flipBits(long n1)        { return ~n1;  }
public long nosign(long n1)        { return n1 *= -1;  }
public long ShiftLeftLong(long n1, int n2)  { return n1 << n2;  }
public long ShiftRightLong(long n1, int n2)  { return n1 >> n2;  }
#End If
#End Region


In B4J I do not know how to do the JavaObject init to get to my functions

B4X:
Private Sub CreateJO As JavaObject
     Dim jo As JavaObject
    
     jo.InitializeNewInstance(Application.PackageName & ".cgenfuncs", Null)   '  How do I do this in B4J
    
     Return jo
End Sub

Little help.
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Figured it out? I think. It is working.

jo.InitializeNewInstance("com.BOBs.MusicDB" & ".cgenfuncs", Null)

Is there a way to get the package name without hard coding - I guess the equivalent to Application
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
In B4J 'Me' refers to the current class, so if your code is added to the default main class (b4j.example.main) then you can use

B4X:
Dim jo as JavaObject = Me
...
jo.RunMethod("andLong",Array(N1,N2))
...

If you get message like "xxx is not instance of calling class" you will need to make the functions static
B4X:
...
public static long andLong(long n1, long n2) { return n1 & n2; }
...
 
Upvote 0
Top