Hello everyone,
This piece of C# code does not produce any overflow exception.
wordsize is a constant equal to 32.
the x and y can be very big at the computation time. So big that long or decimal even cant hold.
But C# handles the problem even the value of the variables exceeds its limits. The computation gives thr right result.(I dont know how it does it)
/**
* Perform a right "spin" of the word. The rotation of the given
* word <em>x</em> is rotated left by <em>y</em> bits.
* Only the <em>lg(wordSize)</em> low-order bits of <em>y</em>
* are used to determine the rotation amount. Here it is
* assumed that the wordsize used is a power of 2.
*
* @param x word to rotate
* @param y number of bits to rotate % wordSize
*/
private int RotateRight(int x, int y)
{
return ((int)(((uint) x >> (y & (wordSize-1))) | (uint)(x << (wordSize - (y & (wordSize-1))))));
}
I tried to convert it into VB.NET like below. But it always gives an overflow exception.
Private Function RotateRight(ByVal x As Integer, ByVal y As Integer) As Integer
Return (CInt(Fix((CUInt(x) >> (y And (wordSize - 1))) Or CUInt(x << (wordSize - (y And (wordSize - 1)))))))
End Function
What can be the reason? How can I solve it?
Can you write a better VB.NET equivalent of the C# code above?