Sub Class_Globals
Public bitS(16) As Int ' need that for quick access'
Private Bits1 As Int = Bit.ShiftLeft(1, 1)
Private Bits2 As Int = Bit.ShiftLeft(1, 2)
Private Bits3 As Int = Bit.ShiftLeft(1 ,3)
Private Bits4 As Int = Bit.ShiftLeft(1, 4) 'ie 2^4 or just 16
Private Bits5 As Int = Bit.ShiftLeft(1, 5)
Private Bits6 As Int = Bit.ShiftLeft(1, 6)
Private Bits7 As Int = Bit.ShiftLeft(1, 7) 'ie 2^7 or just 128
Private Bits8 As Int = Bit.ShiftLeft(1, 8)
Private Bits9 As Int = Bit.ShiftLeft(1, 9)
Private Bits10 As Int = Bit.ShiftLeft(1,10)
Private Bits11 As Int = Bit.ShiftLeft(1,11)
Private Bits12 As Int = Bit.ShiftLeft(1,12)
Private Bits13 As Int = Bit.ShiftLeft(1,13)
Private Bits14 As Int = Bit.ShiftLeft(1,14)
Private Bits15 As Int = Bit.ShiftLeft(1,15)
End sub
Private Sub B4XPage_Created (Root1 As B4XView)
bitS(1) = Bits1 : bitS(2) = Bits2 : bitS(3) = Bits3 : bitS(4) = Bits4 : bitS(5) = Bits5 : bitS(6) = Bits6
bitS(7) = Bits7 : bitS(8) = Bits8 : bitS(9) = Bits9 : bitS(10) = Bits10 : bitS(11) = Bits11 : bitS(12) = Bits12
bitS(13) = Bits13 : bitS(14) = Bits14 : bitS(15) = Bits15
End sub
Private Sub Button1_Click ' set the bit by index 1-14'
Dim indx As Int
indx = TextField1.Text
bitString = SetBit(bitString,indx,True)
DisplayBits(bitString)
End Sub
Private Sub Button2_Click
Dim Result As Boolean
Dim Msg As String
Dim indx As Int
indx = TextField1.Text ' index to test for 1-14'
Result = getByte(bitString,indx)
DisplayBits(bitString)
Log("Result of bit read at index:" & TextField1.Text & " " & Result)
If Result = True Then
Msg = "The bit is set"
Else
Msg = "The bit is not set"
End If
xui.MsgboxAsync(Msg, "BITS")
End Sub
Sub SetBit(b As Int, index As Int, on As Boolean) As Int
If index < 0 Then index = 0
Dim bitValue As Int = bitS(index)
If on Then
b = Bit.Or(b, bitValue)
Else
b = Bit.Or(b, Bit.Not(bitValue))
End If
Return b
End Sub
Sub getByte(bt As Int,index As Int) As Boolean
Dim bstr As String = Bit.ToBinaryString(bt)
Dim b As String
Dim indx As Int
indx = 15 - index
'index = index - 1
If indx < 0 Then
indx = 0
End If
Try
If bstr.SubString2(indx,indx+1) = "0" Then
Return False
else if bstr.SubString2(indx,indx+1) = "1" Then
Return True
End If
Catch
Log(LastException)
End Try
End Sub
Sub DisplayBits(Bt As Int)
' display the value of each bits
Dim nStr As String = ""
Dim on As Boolean
Dim bitValue As Int = Bt
Dim bitStr As String = Bit.ToBinaryString(Bt)
Log("**************************")
Log("Binary format")
Log(bitStr)
Label2.Text = bitStr
End Sub
Sub clearBits() ' clear all the bits to 0 but not the first bit
bitString = 0x8000
DisplayBits(bitString)
End Sub