I just learned that decimal
datatype is represented by 128 bits in .NET
Is it possible to do bitwise operations on decimal
, and will that be any faster/slower than doing the operation on 2 ulong
?
I just learned that decimal
datatype is represented by 128 bits in .NET
Is it possible to do bitwise operations on decimal
, and will that be any faster/slower than doing the operation on 2 ulong
?
No, you can't. First, decimal
is a floating-point type with base 10, not a 128-bit integer. You can't do bitwise operations on floating-point types
Second, there's no 128-bit architecture out there, so even if you managed to have a 128-bit integer type in C# then it's in no way faster than operating on 2 ulong
variables as you still have to break the 128-bit value into smaller pieces that fit into the register
That said, most modern PC architectures have SIMD support so it's possible to have fast bitwise operations on a 128-bit type. It's very easy in native code but I'm not sure if you can do that in C# or not. If not then you must write a native code library and call it from C#.
Nevertheless in C# there's the BitArray class that may use SIMD under the hood so try creating a 128-bit BitArray and see
In Mono there has long been SIMD support with the Mono.Simd
namespace. Later when Microsoft developed RyuJit they introduced Microsoft.Bcl.Simd
for this purpose. It has now been renamed to System.Numerics
which includes
The SIMD-enabled vector types, which include Vector2, Vector3, Vector4, Matrix3x2, Matrix4x4, Plane, and Quaternion.
Mono now also supports the official System.Numerics
namespace. Unfortunately currently many types in that namespace don't have any support for bitwise operations so you may need to work around that using normal arithmetic operations. But the Vector<T>
struct do have BitwiseAnd
, BitwiseOr
, ExclusiveOr
...
For more information read
System.Numerics.Vector
for Graphics Programming