module Bits.Show where

import Data.Bits
import GHC.Exts

class FixedWidthIntegral

{- |
Examples:

@showFiniteBits (5 :: Int8) = "00000101"@

@showFiniteBits ((-5) :: Int8) = "11111011"@
-}
showFiniteBits :: (FiniteBits bits, IsString string) => bits -> string
showFiniteBits :: forall bits string.
(FiniteBits bits, IsString string) =>
bits -> string
showFiniteBits bits
x =
  forall a. IsString a => String -> a
fromString
    (
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool -> Char
showBit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Bits a => a -> Int -> Bool
testBit bits
x)
        (forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Ord a => a -> a -> Bool
>= Int
0)
          (
            forall a. (a -> a) -> a -> [a]
iterate
              (\Int
i -> Int
i forall a. Num a => a -> a -> a
- Int
1)
              (forall b. FiniteBits b => b -> Int
finiteBitSize bits
x forall a. Num a => a -> a -> a
- Int
1)
          )
        )
    )

{- | @{ True -> '1'; False -> '0' }@ -}
showBit :: Bool -> Char
showBit :: Bool -> Char
showBit Bool
x = case Bool
x of
    Bool
True -> Char
'1'
    Bool
False -> Char
'0'