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:

5
  • 1
    What code did you try in R? Commented Jan 27, 2021 at 7: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. Commented Jan 27, 2021 at 7:56
  • Check if linked post works for you. Commented Jan 27, 2021 at 8:01
  • Another possible duplicate: stackoverflow.com/questions/6614283/… Commented Jan 27, 2021 at 8: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)) } Commented Jan 27, 2021 at 8:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.