Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- binStr <- "00000001001100110000010110110111" # 20121015
- (binNum <- 00000001001100110000010110110111) # 20121015
- [1] 1.0011e+24
- binVec <- c(1,0,1,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1) # 2670721
- shortBin <- 10011010010 # 1234
- BinToDec <- function(x)
- sum(2^(which(rev(unlist(strsplit(as.character(x), "")) == 1))-1))
- BinToDec(binStr)
- [1] 20121015
- BinToDec(binNum)
- [1] 576528
- BinToDec(binVec)
- [1] 2670721
- BinToDec(shortBin)
- [1] 1234
- (vec <- digitsBase(5, base= 2, 10))
- Class 'basedInt'(base = 2) [1:1]
- [,1]
- [1,] 0
- [2,] 0
- [3,] 0
- [4,] 0
- [5,] 0
- [6,] 0
- [7,] 0
- [8,] 1
- [9,] 0
- [10,] 1
- BinToDec(vec)
- [1] 5
- (x <- unbinary("10101010"))
- [1] 170
- (y <- binary(x))
- [1] "10101010"
- (yy <- intToBits(5))
- # [1] 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
- # [26] 00 00 00 00 00 00 00
- # Note that there are 32 bits and the order is reversed from your example
- class(yy)
- [1] "raw"
- packBits(yy, "integer")
- # [1] 5
- strtoi("00000001001100110000010110110111", base = 2)
- # [1] 20121015
- strtoi("000101", base = 2)
- # [1] 5
- base::strtoi(binary_string, base = 2)
- base2decimal = function(base_number, base = 2) {
- split_base = strsplit(as.character(base_number), split = "")
- return(sapply(split_base, function(x) sum(as.numeric(x) * base^(rev(seq_along(x) - 1)))))
- }
- > base2decimal(c("000101", "00000001001100110000010110110111"))
- [1] 5 20121015
- bincount <- function(B, base=2) { return(B %*% base^seq(0,ncol(B)-1)) }
- isBig <- c(0, 1, 0, 1)
- isRed <- c(0, 0, 1, 1)
- B = cbind(isBig,isRed)
- bincount(B)
- # 0 1 2 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement