diff --git a/src/Ramstack.HtmxToolkit/Collections/SmallDictionary.cs b/src/Ramstack.HtmxToolkit/Collections/SmallDictionary.cs index fd8df81..a43bd72 100644 --- a/src/Ramstack.HtmxToolkit/Collections/SmallDictionary.cs +++ b/src/Ramstack.HtmxToolkit/Collections/SmallDictionary.cs @@ -370,7 +370,13 @@ private ref KeyValuePair Find(TKey key) while (lo <= hi) { - var mi = (lo + hi) >> 1; + // + // Both lo and hi are non-negative and within [0..int.MaxValue], + // so their sum fits in uint. With unchecked addition, the unsigned + // right shift handles any int wraparound, making this equivalent to + // (int)(((uint)lo + (uint)hi) >> 1), but cleaner and easier to read. + // + var mi = (lo + hi) >>> 1; if ((uint)mi >= (uint)array.Length) break; @@ -424,7 +430,13 @@ private int IndexOf(TKey key) while (lo <= hi) { - var mi = lo + (hi - lo >>> 1); + // + // Both lo and hi are non-negative and within [0..int.MaxValue], + // so their sum fits in uint. With unchecked addition, the unsigned + // right shift handles any int wraparound, making this equivalent to + // (int)(((uint)lo + (uint)hi) >> 1), but cleaner and easier to read. + // + var mi = (lo + hi) >>> 1; if ((uint)mi >= (uint)array.Length) break;