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

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

What do I need to import to do bitwise operations?

问题

以下是代码的翻译部分:

我尝试使用位运算符,但我无法弄清楚我需要导入什么。

import Data.Word
import Data.Bits

mask = 0xff :: Word64
v = 98213 :: Word64

lsb = mask .&. v

这是我看到的内容:

ghci tmp.hs 
GHCi,版本9.2.8https://www.haskell.org/ghc/  :? 获取帮助
[1 of 1] Compiling Main             ( tmp.hs, interpreted )

tmp.hs:7:12: 错误:
    变量不在范围内:(&) :: Word64 -> Word64 -> t
  |
7 | lsb = mask & v
  |            ^
失败,未加载任何模块。
ghci>

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

英文:

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

$ cat tmp.hs
import Data.Word
import Data.Bits

mask = 0xff :: Word64
v = 98213 :: Word64

lsb = mask & v

And this is what I see:

$ ghci tmp.hs 
GHCi, version 9.2.8: https://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( tmp.hs, interpreted )

tmp.hs:7:12: error:
    Variable not in scope: (&) :: Word64 -> Word64 -> t
  |
7 | lsb = mask & v
  |            ^
Failed, no modules loaded.
ghci> 

答案1

得分: 4

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

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:

确定