hm ...
I added a request to the wishlist: JSL bit operations
(@jthi already requested in 2020)
For now, (Jmp18), what is the fastest way to extract the 32 bits of a 32bit number?
... for 10 mio rows?
n_rows = 100000;
dt = New Table( "test", Add Rows( n_rows ), New Column( "rand int", Set each value( Random Integer( 0, 4294967295 ) ) ) );
checkBit = Function( {x, bit},
(Mod( Floor( x / 2 ^ (0 + bit) ), 2 ) == 1)
);
t0 = HP Time();
For( i = 1, i <= 32, i++,
dt << New Column( "bit" || Char(i), Set each value( checkBit( :rand int, i ) ) )
);
Show( (HP Time() - t0) / 1000000 ); // references
//BITS:NUMTOBITS from https://community.jmp.com/t5/Uncharted/7-things-to-know-about-Twitter/ba-p/21000
// faster
getBits = Function( {x},
{result = {}, i},
For( i = 4294967296 / 2, i >= 1, i /= 2,
Insert Into(
result,
If( x >= i,
x = x - i;
1;
,
0
)
)
);
Transpose(Matrix(result));
);
// ~ factor 10 faster
getBits = Function( {x},
Transpose(Matrix(Transform Each({bit}, Words(Hex( x, Base( 2 ), Pad To( 32 ) ),""),Num(bit))));
);
//getBits(4294967295);
For( i = 1, i <= 32, i++,
New Column( "bit" || Char( i ) )
);
t0 = HP Time();
mat = J( n_rows, 32, 0 );
For( myrow = 1, myrow <= n_rows, myrow++,
mat[myrow, 0] = getBits( :rand int[myrow] )
);
dt[0, N Cols( dt ) - 31 :: N Cols( dt )] = mat;
Show( (HP Time() - t0) / 1000000 );