Comment #0 by bearophile_hugs — 2014-07-31T20:13:57Z
The purpose of checkedint functions/intrinsics is to detect overflows and bugs.
When you have a number x of type int/uint/long/ulong where one or more of the most n significant bits is set to 1, and you shift x on left by n, you lose some information, so you have an overflow.
So I suggest to add to the checkedint.d module the leftShift functions that perform a checked left shifting. They should also warn against too much large shifts:
leftShift(in uint x, in uint n, ref bool overflow)
leftShift(in ulong x, in uint n, ref bool overflow)
They check that:
1) n is in the appropriate range [0, sizeof(x) * 8]. Otherwise they set overflow to true.
2) None of the n most significant bits of x is 1 before the shifting.
Comment #1 by robert.schadek — 2024-12-07T13:34:03Z