I don't think you really need to use the bit operator for this, but if for some reason you should (for example, this is a school question):
First, you can use Decimal.GetBits() to get all the bits in two Decimals for comparison, like an array of 4 integers.
Then you can check the sign bit, which is at bit 31 in int with offset 3 in the ints array.
Decimal d1 = 1; Decimal d2 = -1; var bits1 = Decimal.GetBits(d1); var bits2 = Decimal.GetBits(d2); const int signMask = 1 << 31; const int signWord = 3; bool sameSign = ((bits1[signWord] & signMask) == (bits2[signWord] & signMask));
Matthew watson
source share