Hi i want to set or reset a bit of an integer and read back that bit in the same integer to tell me if the bit is set or not set
Ex: i want to set the bit 1 and 7 of an integer of 0X0 does not matter what the value of the initial integer is but as a start it will be to 0 but when reading a value from a file that integer will be set to whatever the value that correspond to bit 1 and 7 bit set ,
I want to be able to pass that number to a bit reader that will tell me if the bit 1 or the bit 7 is set or it can be both 1 and 7.
I tried with Bit.Or it set the integer value ok but i can't seem to read the bit setting as it return always false
On that specific integer i have up to 12 bits to set or not that correspond to some value when it is interpreted in B4A, B4I, or B4J
it could have more then 1 position to be set but mostly 1 or 2 bit will be set at the same time.
Here is some code that i use it is only for testing
Can someone help me with that i don't know what i'm doing wrong to get it work
Test set and read the bit of an integer
Ex: i want to set the bit 1 and 7 of an integer of 0X0 does not matter what the value of the initial integer is but as a start it will be to 0 but when reading a value from a file that integer will be set to whatever the value that correspond to bit 1 and 7 bit set ,
I want to be able to pass that number to a bit reader that will tell me if the bit 1 or the bit 7 is set or it can be both 1 and 7.
I tried with Bit.Or it set the integer value ok but i can't seem to read the bit setting as it return always false
On that specific integer i have up to 12 bits to set or not that correspond to some value when it is interpreted in B4A, B4I, or B4J
it could have more then 1 position to be set but mostly 1 or 2 bit will be set at the same time.
Here is some code that i use it is only for testing
Can someone help me with that i don't know what i'm doing wrong to get it work
Test set and read the bit of an integer
bit set and read:
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Public bitString As Long = 0x0 ' this value will be stored in a file and the value should correspond to the bits set in that integer
Public jo As JavaObject
Private Label1 As Label
Private TextField1 As TextField
Private Button2 As Button
Private Label2 As Label
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
jo = Me
End Sub
Private Sub Button1_Click this set the bit by index value ex:1 or 7 or both
Dim indx As long
indx = TextField1.Text ' either 1 or 7 or other bit number
bitString = setBit(indx,bitString)
Label2.Text = bitString
End Sub
Private Sub Button2_Click ' this read the value of a specific bit in the integer
Dim Result As Boolean
Dim Msg As String
Dim indx As long
indx = TextField1.Text
Result = readBit(indx,bitString)
Log("Result of bit read at index:" & TextField1.Text & " " & Result)
Label2.Text = bitString
If Result = True Then
Msg = "The bit is set"
Else
Msg = "The bit is not set"
End If
xui.MsgboxAsync(Msg, "BITS")
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
Sub GetBitLong (bstr As Long, index As Int) As Boolean
Log("reading:" & bstr)
Dim t As Long = ShiftLeftLong(bstr,index)
Return AndLong(bstr,t) = t
End Sub