Regarding your edited question with:
uint amountToShift2 = 12;
BigInteger num = new BigInteger(-126);
uint compactBitsRepresentation = (uint)(num >> (int)amountToShift2);
The bit shift works OK and produces a BigInteger
of value -1
(negative one).
But the conversion to uint
throws an exception becauce -1
is outside the range of an uint
. The conversion from BigInteger
to uint
does not "wrap around" modulo 2**32
, but simply throws.
You can get around that with:
uint compactBitsRepresentation = (uint)(int)(num >> (int)amountToShift2);
which will not throw in unchecked
context (which is the usual context).