# Calculate bits and bytes

Discussion in 'Questions (Windows Mobile)' started by schimanski, Aug 29, 2008.

1. ### schimanskiWell-Known MemberLicensed 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

2. ### agrahamExpertLicensed 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.
Code:
`Sub Globals   Dim County   Dim DistrictEnd SubSub App_Start   Decode(128 + 3 * 8 + 12 /4)   msg = "County - " & County & CRLF   msg = msg & "District - " & District    Msgbox(msg)End SubSub Decode(code)   code = code - 128   District =  4 * (code Mod 8) ' bottom three bits   County = Int(Code / 8)End Sub`

Last edited: Aug 29, 2008
3. ### schimanskiWell-Known MemberLicensed 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: