This is O (log (n)) because you have to divide by 2 every time you are going to print 0 or 1 . For example, to print 256 in binary format, you would have to divide by 2, starting at 256, and print the result % 2 each time.
256 % 2 -> 0 64% 2 -> 0 32 % 2 -> 0 16 % 2 -> 0 8 % 2 -> 0 4 % 2 -> 0 2 % 2 -> 0 1 % 2 -> 1
So, for some 256 you will need to repeat 8 times, equal to log 256 .
source share