i have some area flags in hex format e.g. "090A", "3BFF", "0400", "2025".
In SQL I can use the following syntax to work out the overlap between 2 area flags, for example:
SELECT
STRTOL('090A', 16) as area1
,STRTOL('3BFF', 16) as area2
,STRTOL('090A', 16) & STRTOL('3BFF', 16) as overlap;
area1 | area2 | overlap |
---|---|---|
2314 | 15359 | 2314 |
this shows that area1 is fully included in area2.
Another example:
SELECT
STRTOL('0400', 16) as area3
,STRTOL('2025', 16) as area4
,STRTOL('0400', 16) & STRTOL('2025', 16) as overlap;
area3 | area4 | overlap |
---|---|---|
1024 | 8229 | 0 |
this shows that there is no overlap between area3 and area4.
I see we can use R.utils::strtoi
and that gives me the same conversion for each area that I get in SQL. However if i use the syntax
strtoi("0400", 16L) & strtoi("2025", 16L)
I get TRUE
even when there is no overlap. Is there a way to get the overlap in R using strtoi
like we can in SQL?
You can use the function bitwAnd()
to compute the bitwise and:
bitwAnd(strtoi("0400", 16L), strtoi("2025", 16L))
## [1] 0
bitwAnd(strtoi("090A", 16L), strtoi("3BFF", 16L))
## [1] 2314
The reason that this
strtoi("0400", 16L) & strtoi("2025", 16L)
gives TRUE
is the following: &
is the logical and-operator in R. And numbers larger than 0 are converted to TRUE
. So your code is equivalent to
TRUE & TRUE
which evaluates to TRUE
.