0

I learned the following piece of C++ code about binary conversion and tried to translate it to R code, but not succeeded yet. Does anyone know how to do it?

for(int i = 0; i < 16: ++i) {
    printf("%u%u%u%u", i/8%2, i/4%2, i/2%2, i%2);  
}

C++ code is from below accepted answer from below post:

zx8754
  • 52,746
  • 12
  • 114
  • 209
user7283235
  • 101
  • 5
  • 1
    What code did you try in R? – RoQuOTriX Jan 27 '21 at 07:55
  • Show what you tried. While this is _technically_ C++ code, it's not really. And it seems to be missing a newline in the output, unless that was intentional. – paddy Jan 27 '21 at 07:56
  • Check if linked post works for you. – zx8754 Jan 27 '21 at 08:01
  • Another possible duplicate: https://stackoverflow.com/questions/6614283/converting-decimal-to-binary-in-r – MrFlick Jan 27 '21 at 08:08
  • Thanks, but those posts did not provide hints to translate C++ code to R code. Fortunately, I got it working now... for(i in 0:15) { print(c(floor(i/8)%%2, floor(i/4)%%2, floor(i/2)%%2, floor(i)%%2)) } – user7283235 Jan 27 '21 at 08:35

0 Answers0