Three word location: Difference between revisions

Content added Content deleted
(→‎{{header|Raku}}: Make word indexing routine user overridable, demo)
m (→‎{{header|Julia}}: bit twiddles)
Line 168: Line 168:


=={{header|Julia}}==
=={{header|Julia}}==
Direct translation from the SymSyn example given by the task creator. though note that idiomatic Julia would usually code this as two small encode() and decode() functions.
Direct translation from the SymSyn example given by the task creator, though note that idiomatic Julia would usually code this as two small encode() and decode() functions.
<lang julia>
<lang julia>
# Three Word Location - convert latitude and longitude to three words
# Three Word Location - convert latitude and longitude to three words
Line 187: Line 187:
# next 14 bits for word 2 index
# next 14 bits for word 2 index
# next 14 bits for word 3 index
# next 14 bits for word 3 index
W1 = (LATLON >> 28) & 0xefff
W1 = (LATLON >> 28) & 0x7fff
W2 = (LATLON >> 14) & 0x7fff
W2 = (LATLON >> 14) & 0x3fff
W3 = LATLON & 0x7fff
W3 = LATLON & 0x3fff


# fetch each word from word array
# fetch each word from word array
Line 241: Line 241:
end
end
i = (Int(lat * 10000 + 900000) << 22) | Int(lon * 10000 + 1800000)
i = (Int(lat * 10000 + 900000) << 22) | Int(lon * 10000 + 1800000)
return map(x -> arr[x + 1], [(i >> 28) & 0xefff, (i >> 14) & 0x7fff, i & 0x7fff])
return map(x -> arr[x + 1], [(i >> 28) & 0x7fff, (i >> 14) & 0x3fff, i & 0x3fff])
end
end