F# 中的位移位

Bit shift in F#

我是 F# 的新手,不了解 F# 中位移的工作原理。

我在 fsi 中尝试了下面的命令。

> 4
- |>((<<<) 1uy);;

截图如下。

为什么这个结果是 16uy 而不是 8uy?

当我尝试下面的命令时甚至更加困惑,因为结果是 48uy...

> 4
- |>((<<<) 3uy);;

有人能告诉我这是怎么回事吗?

来自the documentation

Bitwise left-shift operator. The result is the first operand with bits shifted left by the number of bits in the second operand. Bits shifted off the most significant position are not rotated into the least significant position. The least significant bits are padded with zeros. The type of the second argument is int32.

自从我使用 F# 以来已经有一段时间了,但假设它的运算符前缀像 Haskell 一样工作,那么您使用它的方式是:

4 |> ((<<<) 1uy)

将应用 1 作为左侧参数,将 4 作为右侧参数:

1 <<< 4

这将是 16

要使其等于 8,请尝试删除运算符本身周围的括号(这意味着它不会添加前缀,而只是部分应用右侧参数)以得到:

4 |> (<<< 1uy)

假设它是有效的 F# 语法,那应该会给你 8