I'm trying to convert an integer value to its bit expression and convert parts of that again to integer (as per this blogpost, for context).
Transforming integers to bits does seem to work, but I'm missing something when transforming the values back into the initial integer.
'intToBase2' <- function(x){
x %>%
intToBits() %>%
rev %>%
as.character() %>%
{sapply(strsplit(., "", fixed = TRUE), '[', 2)} %>%
paste0(.,collapse = '')
}
val <- 1855928457
intToBase2(val) # does seem to return the correct bit expression, as expected
However, if I try to reverse the same logic, something gets lost somewhere:
val %>%
intToBase2 %>% # Get expression in bits
{strsplit(.,'')[[1]]} %>% # split
sprintf('0%s',.) %>% # add leading zeros
rev %>% # reverse order
as.raw %>% # expression to raw
readBin(.,what='integer')
R> [1] 16777217
What am I missing? I assume some of the steps I did are incorrect.