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?

You're probably going over the maximum value of an integer. Consider the following two code samples.

static void Main(string[] args)
        {
            int max = int.MaxValue;
            max++;

            Console.WriteLine(max);
            Console.Read();
        }
Sub Main()

        Dim max As Integer = Integer.MaxValue
        max += 1

        Console.WriteLine(max)
        Console.Read()

    End Sub

Aside from language, the two code pieces are identical. The VB version will throw an overflow exception. The C# version executes without a problem. Why?

The answer is that the C# executes with a hidden problem. If you exceed the maximum value of an integer in C#, it wraps back around to the minimum value!

You say your number is too big for a long, but that your integer version gives you the correct result. Personally, I would revisit that assumption.

VB.NET has a project level setting to check for overflow. (Project properties + Compile Tab + Advanced Compiler Option + Chech "Remove integer overflow checks")

C# statements can run in either a checked or an unchecked context. In a checked context, arithmetic overflow raises an exception error. In an unchecked context, arithmetic overflow is ignored and the result is truncated.

thanks for both explanations.

But how can I solve it?

@apegram

C# wraps (Integer.MaxValue + Add) to Integer.MinValue + (Add-1).

How Can I make this manually in VB.NET

I have to find the difference. While calculating the difference, I have to make an adding and I think it will give an overflow again. How can I do it?

@ adatapost
yes these checked/unchecked statements dont exist in vb.net. How can I solve it manually? How can manually truncate it?

This question has already been answered. Start a new discussion instead.