英文:
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.8:https://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>.&.</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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论