/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/runtime-libraries/snippets/System/Object/GetHashCode/fsharp/shift1.fs
50 строк
2 KB
Genevieve Warren
Move member remarks (#39270)
30 янв 2024, 00:43
Не верифицирован
30 янв 2024, 00:43
6c88824
Код
Авторство
О чём код?
module shift1 // <Snippet5> open System [<Struct; CustomEquality; NoComparison>] type Point(x: int, y: int) = member _.X = x member _.Y = y override _.Equals(obj) = match obj with | :? Point as p -> x = p.X && y = p.Y | _ -> false override this.GetHashCode() = this.ShiftAndWrap(x.GetHashCode(), 2) ^^^ y.GetHashCode() member _.ShiftAndWrap(value, positions) = let positions = positions &&& 0x1F // Save the existing bit pattern, but interpret it as an unsigned integer. let number = BitConverter.ToUInt32(BitConverter.GetBytes value, 0) // Preserve the bits to be discarded. let wrapped = number >>> (32 - positions) // Shift and wrap the discarded bits. BitConverter.ToInt32(BitConverter.GetBytes((number <<< positions) ||| wrapped), 0) let pt = Point(5, 8) printfn $"{pt.GetHashCode()}" let pt2 = Point(8, 5) printfn $"{pt2.GetHashCode()}" // The example displays the following output: // 28 // 37 // </Snippet5> // <Snippet4> let shiftAndWrap (value: int) positions = let positions = positions &&& 0x1F // Save the existing bit pattern, but interpret it as an unsigned integer. let number = BitConverter.ToUInt32(BitConverter.GetBytes value, 0) // Preserve the bits to be discarded. let wrapped = number >>> (32 - positions) // Shift and wrap the discarded bits. BitConverter.ToInt32(BitConverter.GetBytes((number <<< positions) ||| wrapped), 0) // </Snippet4>