Skip to content

Instantly share code, notes, and snippets.

@hadronized
Last active January 26, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadronized/99df1626d8709d55b6526e9c19d154e2 to your computer and use it in GitHub Desktop.
Save hadronized/99df1626d8709d55b6526e9c19d154e2 to your computer and use it in GitHub Desktop.
toBin8 :: Int8 -> Integer
toBin8 x =
y .&. 1
+ (y `shiftR` 1 .&. 1) * 10
+ (y `shiftR` 2 .&. 1) * 100
+ (y `shiftR` 3 .&. 1) * 1000
+ (y `shiftR` 4 .&. 1) * 10000
+ (y `shiftR` 5 .&. 1) * 100000
+ (y `shiftR` 6 .&. 1) * 1000000
+ (y `shiftR` 7 .&. 1) * 10000000
where y = fromIntegral x
toBin16 x = (toBin8 (fromIntegral $ x `shiftR` 8) * 100000000) + toBin8 (fromIntegral $ x .&. 0xFF)
toBin32 x = (toBin16 (fromIntegral $ x `shiftR` 16) * 10000000000000000) + toBin16 (fromIntegral $ x .&. 0xFFFF)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment