Skip to content
Prev 278990 / 398513 Next

How to speed up int2bin conversion?

If we assume that you are just convert 8 bits, here is one way with a
table lookup.  If more than 8 bits, just partition the data and
repeat.  This sets up a mapping table one time for the lookup.  Does
1M in 0.3 seconds on my computer; is this fast enough?
+     b2c[i + 1] <- sprintf("%1d%1d%1d%1d%1d%1d%1d%1d"
+         , bitAnd(i, 0x80) != 0
+         , bitAnd(i, 0x40) != 0
+         , bitAnd(i, 0x20) != 0
+         , bitAnd(i, 0x10) != 0
+         , bitAnd(i, 0x8) != 0
+         , bitAnd(i, 0x4) != 0
+         , bitAnd(i, 0x2) != 0
+         , bitAnd(i, 0x1) != 0
+         )
+ }
+ {
+     b2c[bitAnd(val, 0xff) + 1]
+ }
user  system elapsed
   0.31    0.00    0.32
On Thu, Dec 1, 2011 at 7:14 AM, Jonas J?germeyr <jonasjae at pik-potsdam.de> wrote: