Android Question Bit.AND and Bit.Or for Longs

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Does anyone have a library that will allow modifying then bits in long values

I want to be able to use a long (64 bits) for flags and the Bit operations only return longs.

Has anyone made a library that works with longs?

BobVal
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Create a class module with this code:
B4X:
Sub Class_Globals
   Private jo As JavaObject
End Sub

Public Sub Initialize
   jo = Me
End Sub

public Sub AndLong (N1 As Long, N2 As Long) As Long
   Return jo.RunMethod("andLong", Array(N1, N2))
End Sub

public Sub OrLong (N1 As Long, N2 As Long) As Long
   Return jo.RunMethod("orLong", Array(N1, N2))
End Sub

#If JAVA
public long andLong (long n1, long n2) {
   return n1 & n2;
}
public long orLong (long n1, long n2) {
   return n1 | n2;
}
#End If
 
Upvote 0

Marc De Loose

Member
Licensed User
Longtime User
I think this is related.
I am using this code to read flags

B4X:
Sub GetBit(b As Byte, index As Int) As Boolean
    Dim t As Byte = Bit.ShiftLeft(1, index)
    Return Bit.And(b, t) = t
End Sub

But now I need to read the flags of a bitfield that is basicaly an unsigned int (max value 4294967295)

Any pointers on how I can change the Sub GetBIt so I can Use it with unsigned and unsigned int (I guess a long should work too)?

That would be a great help.

thx
 
Upvote 0

Marc De Loose

Member
Licensed User
Longtime User
I think I figured it out. This seems to be working:
B4X:
Sub Class_Globals
    Private jo As JavaObject
End Sub

Public Sub Initialize
    jo = Me
End Sub

public Sub AndLong (N1 As Long, N2 As Long) As Long
    Return jo.RunMethod("andLong", Array(N1, N2))
End Sub

public Sub OrLong (N1 As Long, N2 As Long) As Long
    Return jo.RunMethod("orLong", Array(N1, N2))
End Sub

public Sub ShiftLeftLong (N1 As Long, Index As Int) As Long
    Return jo.RunMethod("shiftLeftLong",Array(N1,Index))
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 shiftLeftLong (long n1, int ind) {
    return n1 << ind;
}
#End If

and the new GetBitLong:

B4X:
sub GetBitLong (l As Long, index As Int) As Boolean
    Dim t As Long = longbit.ShiftLeftLong(1,index)
    Return longbit.AndLong(l,t) = t
End Sub
 
Upvote 0
Top