4 ms·
I think it’s the language. Haven’t really tested but in C# that would be something like that, 8 lines of code instead of 40: public static byte[] func( uin
by Const-me 5y ago
I think it’s the language. Haven’t really tested but in C# that would be something like that, 8 lines of code instead of 40:
public static byte[] func( uint p, uint q )
{
int pw = 32 - BitOperations.LeadingZeroCount( p );
int qw = 32 - BitOperations.LeadingZeroCount( q );
Span<byte> buff = stackalloc byte[ 9 ];
ulong val = (ulong)q | (ulong)p << qw;
BinaryPrimitives.WriteUInt64BigEndian( buff.Slice( 1 ), val );
int totalBytes = ( pw + qw + 7 ) / 8;
buff[ 8 - totalBytes ] = (byte)qw;
return buff.Slice( 8 - totalBytes ).ToArray();
}
In C++ about the same, albeit you gonna need either C++/20 language version, or some non-standard compiler specific intrinsics for bit scan and byte swap.
- marginalia_nu 5y agoYeah, Java is almost certainly not the optimal language for low level programming. It's like C++ with oven mitts.