Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell bitwise operators in Data.Bits

I am new to Haskell and am trying to use bitwise operations from Data.Bits. Every time I try I get an error message

Prelude Data.Bits> 1 `shiftL` 16

<interactive>:1:0:
    Ambiguous type variable `t' in the constraint:
      `Bits t' arising from a use of `shiftL' at <interactive>:1:0-12
    Probable fix: add a type signature that fixes these type variable(s)

This happens for a number of operations, I also tried .|. and .&.

I must be missing something very simple, please let me know if you can spot the problem

like image 892
pu239ppy Avatar asked Mar 02 '26 01:03

pu239ppy


1 Answers

In the interactive session, Haskell can't infer the types of 1 and 16. The solution then, is to give a hint:

> :m +Data.Bits
> let a = 1 :: Int
> let b = 16 :: Int
> a `shiftL` b
65535
>
like image 50
zdav Avatar answered Mar 04 '26 05:03

zdav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!