你需要导入什么来执行位运算?

huangapple go评论112阅读模式
英文:

What do I need to import to do bitwise operations?

问题

以下是代码的翻译部分:

  1. 我尝试使用位运算符,但我无法弄清楚我需要导入什么。
  2. import Data.Word
  3. import Data.Bits
  4. mask = 0xff :: Word64
  5. v = 98213 :: Word64
  6. lsb = mask .&. v
  7. 这是我看到的内容:
  8. ghci tmp.hs
  9. GHCi,版本9.2.8https://www.haskell.org/ghc/ :? 获取帮助
  10. [1 of 1] Compiling Main ( tmp.hs, interpreted )
  11. tmp.hs:7:12: 错误:
  12. 变量不在范围内:(&) :: Word64 -> Word64 -> t
  13. |
  14. 7 | lsb = mask & v
  15. | ^
  16. 失败,未加载任何模块。
  17. ghci>

请注意,我已将原始代码中的&改为了正常的.&.以修复错误。

英文:

I'm trying to use bitwise operators and I can't figure out what I need to import.

  1. $ cat tmp.hs
  2. import Data.Word
  3. import Data.Bits
  4. mask = 0xff :: Word64
  5. v = 98213 :: Word64
  6. lsb = mask & v

And this is what I see:

  1. $ ghci tmp.hs
  2. GHCi, version 9.2.8: https://www.haskell.org/ghc/ :? for help
  3. [1 of 1] Compiling Main ( tmp.hs, interpreted )
  4. tmp.hs:7:12: error:
  5. Variable not in scope: (&) :: Word64 -> Word64 -> t
  6. |
  7. 7 | lsb = mask & v
  8. | ^
  9. Failed, no modules loaded.
  10. ghci>

答案1

得分: 4

已经导入了这个。但是在Haskell中,位运算操作符是(.&.) :: Bits a => a -> a -> a, (.|.) :: Bits a => a -> a -> a(.^.) :: Bits a => a -> a -> a,所以:

  1. lsb = mask .&. v
英文:

You already imported this. But in Haskell, the bitwise operators are (.&.) :: Bits a => a -> a -> a, (.|.) :: Bits a => a -> a -> a, and (.^.) :: Bits a => a -> a -> a, so:

<pre><code>lsb = mask <b>.&amp;.</b> v</code></pre>

答案2

得分: 1

I think you meant to use [.&.] from Data.Bits. The & operand is the reverse of $ in Haskell from base

英文:

I think you meant to use .&. from Data.Bits. The &amp; operand is the reverse of $ in Haskell from base

huangapple
  • 本文由 发表于 2023年8月11日 00:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定