Created
November 1, 2012 13:53
-
-
Save yoavram/3993746 to your computer and use it in GitHub Desktop.
a function that transforms a vector of bits ( c(0,1,1,0,0) ) to an unsigned integer (6). Includes a test as an example at the bottom.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bits.to.uint <- function(bits) { | |
s=0 | |
for (i in 1:length(bits)) { | |
s = s + 2^(i-1) * bits[i] | |
} | |
return(s) | |
} | |
cat(bits.to.uint( c(0,1,1,0,0) ) == 6) | |
cat(bits.to.uint( c(0,1,0,0,1) ) == 18 ) | |
cat(bits.to.uint( c(1,0,0,0,0,0,0,0,1) ) == 257 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Vectorisation is your friend: