Calculate bits and bytes

schimanski

Well-Known Member
Licensed User
Longtime User
Hello!

I need to decode a UKSP-String (Ultra Short (kurz) String Police) for my application. The UKSP-String is a String with crlf+18 Bytes+crlf. Each Byte will save informations. For example: the first Byte is caluculated with:

Byte1=CHR(128+(County*8)+(bit.AND(District,28))/4))

County and District are decimal values. Now my question: Is there a way to decode the Byte1 in the Country and District? My first problem is to reverse the bitwise operation "bit.AND(District,28)".

Thanks for answer...

schimanski
 

agraham

Expert
Licensed User
Longtime User
I think this does it. What is happening is that the 128 is packing to make the code printable, or at least non-zero. County is multiplied by 8 which effectively left shifts it three binary places leaving the values 0 to 7 free for the District. The District number is ANDed with binary 11100 (28) then divided by 4 which effectively right shifts the result to the the bottom three bit positions left clear by moving County left. As a result the decoded County can have any value but the decoded District can only have one of the eight values 0,4,8,12,16,20,24,28 or 0 to 7 if you don't undo the original divide by 4.
B4X:
Sub Globals
   Dim County
   Dim District
End Sub

Sub App_Start
   Decode(128 + 3 * 8 + 12 /4)
   msg = "County - " & County & CRLF
   msg = msg & "District - " & District 
   Msgbox(msg)
End Sub


Sub Decode(code)
   code = code - 128
   District =  4 * (code Mod 8) ' bottom three bits
   County = Int(Code / 8)
End Sub
 
Last edited:

schimanski

Well-Known Member
Licensed User
Longtime User
Many thanks

Thanks agraham for the fast answer!

I think, that I begin to understand it.

I will try now something with your code...

:sign0060:
 
Top