Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. binStr <- "00000001001100110000010110110111" # 20121015
  2. (binNum <- 00000001001100110000010110110111) # 20121015
  3. [1] 1.0011e+24
  4. 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
  5. shortBin <- 10011010010 # 1234
  6. BinToDec <- function(x)
  7. sum(2^(which(rev(unlist(strsplit(as.character(x), "")) == 1))-1))
  8. BinToDec(binStr)
  9. [1] 20121015
  10. BinToDec(binNum)
  11. [1] 576528
  12. BinToDec(binVec)
  13. [1] 2670721
  14. BinToDec(shortBin)
  15. [1] 1234
  16.  
  17. (vec <- digitsBase(5, base= 2, 10))
  18. Class 'basedInt'(base = 2) [1:1]
  19. [,1]
  20. [1,] 0
  21. [2,] 0
  22. [3,] 0
  23. [4,] 0
  24. [5,] 0
  25. [6,] 0
  26. [7,] 0
  27. [8,] 1
  28. [9,] 0
  29. [10,] 1
  30. BinToDec(vec)
  31. [1] 5
  32.  
  33. (x <- unbinary("10101010"))
  34. [1] 170
  35. (y <- binary(x))
  36. [1] "10101010"
  37.  
  38. (yy <- intToBits(5))
  39. # [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
  40. # [26] 00 00 00 00 00 00 00
  41. # Note that there are 32 bits and the order is reversed from your example
  42.  
  43. class(yy)
  44. [1] "raw"
  45.  
  46. packBits(yy, "integer")
  47. # [1] 5
  48.  
  49. strtoi("00000001001100110000010110110111", base = 2)
  50. # [1] 20121015
  51.  
  52. strtoi("000101", base = 2)
  53. # [1] 5
  54.  
  55. base::strtoi(binary_string, base = 2)
  56.  
  57. base2decimal = function(base_number, base = 2) {
  58. split_base = strsplit(as.character(base_number), split = "")
  59. return(sapply(split_base, function(x) sum(as.numeric(x) * base^(rev(seq_along(x) - 1)))))
  60. }
  61. > base2decimal(c("000101", "00000001001100110000010110110111"))
  62. [1] 5 20121015
  63.  
  64. bincount <- function(B, base=2) { return(B %*% base^seq(0,ncol(B)-1)) }
  65.  
  66. isBig <- c(0, 1, 0, 1)
  67. isRed <- c(0, 0, 1, 1)
  68. B = cbind(isBig,isRed)
  69. bincount(B)
  70. # 0 1 2 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement