Code Snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/ usr / bin / env stack
{- stack
--resolver lts-12.0
--install-ghc
script
--
--ghc-options -Werror
--ghc-options -Wall
-}
module Main ( main ) where
import Data.Bits
( ( .&. )
, shiftR
)
import Data.Word
( Word8
)
--------------------------------------------------------------------------------
toBit'
:: ( Char , Char )
-> Word8
-> String
toBit
:: Word8
-> String
toHex'
:: ( Int , Int )
-> Word8
-> String
toHex
:: Word8
-> String
--------------------------------------------------------------------------------
main
:: IO ()
--------------------------------------------------------------------------------
( .>. ) x y = x ` shiftR ` y
toBit' ( f , s ) x =
case reverse . aux $ x of
[ ] -> rep 8 f []
bin -> ( rep m f [] ) ++ bin
where
n = length bin
m = 8 - n
where
aux 0 = []
aux n =
r : aux c
where
c = n .>. 1
r = if ( n - c * 2 ) == 0 then f else s
rep 0 _ xs = xs
rep n x xs = x : rep ( n - 1 ) x xs
toBit =
toBit' ( '0' , '1' )
toHex' ( f , s ) b =
( aux $ b .>. 0x04 ) : ( aux $ b .&. 0x0F ) : []
where
aux x
| x < 0x0A = toEnum $ fromIntegral $ ( + f ) $ fromIntegral x
| x < 0x10 = toEnum $ fromIntegral $ ( + s ) $ fromIntegral x
| otherwise = error "Shouldn't be possible to reach"
toHex =
toHex' ( 48 , 55 )
split :: Int -> [ a ] -> [[ a ]]
split n =
aux 1 [] []
where
aux _ acc bcc [ ] = acc
aux i acc bcc ( x : xs ) =
if
i == n
then
aux 1 ( acc ++ [ bcc ++ [ x ]]) [] xs
else
aux j acc ( bcc ++ [ x ]) xs
where
j = i + 1
--------------------------------------------------------------------------------
main =
do
putStrLn "-- Bytes as bits:"
mapM_ putStrLn $ map concat $ split 8 $ map toBit bs
putStrLn "-- Bytes as bits (alt):"
mapM_ putStrLn $ map concat $ split 8 $ map ( toBit' ( ' \9633 ' , ' \9632 ' )) bs
putStrLn "-- Bytes as hex:"
mapM_ putStrLn $ map concat $ split 32 $ map toHex bs
putStrLn "-- Bytes as hex (alt):"
mapM_ putStrLn $ map concat $ split 32 $ map ( toHex' ( 9532 , 9532 )) bs
where
bs = [ 0 .. 255 ]
Output:
user@personal:~/.../bitwise$ ./Main.hs
-- Bytes as bits:
0000000000000001000000100000001100000100000001010000011000000111
0000100000001001000010100000101100001100000011010000111000001111
0001000000010001000100100001001100010100000101010001011000010111
0001100000011001000110100001101100011100000111010001111000011111
0010000000100001001000100010001100100100001001010010011000100111
0010100000101001001010100010101100101100001011010010111000101111
0011000000110001001100100011001100110100001101010011011000110111
0011100000111001001110100011101100111100001111010011111000111111
0100000001000001010000100100001101000100010001010100011001000111
0100100001001001010010100100101101001100010011010100111001001111
0101000001010001010100100101001101010100010101010101011001010111
0101100001011001010110100101101101011100010111010101111001011111
0110000001100001011000100110001101100100011001010110011001100111
0110100001101001011010100110101101101100011011010110111001101111
0111000001110001011100100111001101110100011101010111011001110111
0111100001111001011110100111101101111100011111010111111001111111
1000000010000001100000101000001110000100100001011000011010000111
1000100010001001100010101000101110001100100011011000111010001111
1001000010010001100100101001001110010100100101011001011010010111
1001100010011001100110101001101110011100100111011001111010011111
1010000010100001101000101010001110100100101001011010011010100111
1010100010101001101010101010101110101100101011011010111010101111
1011000010110001101100101011001110110100101101011011011010110111
1011100010111001101110101011101110111100101111011011111010111111
1100000011000001110000101100001111000100110001011100011011000111
1100100011001001110010101100101111001100110011011100111011001111
1101000011010001110100101101001111010100110101011101011011010111
1101100011011001110110101101101111011100110111011101111011011111
1110000011100001111000101110001111100100111001011110011011100111
1110100011101001111010101110101111101100111011011110111011101111
1111000011110001111100101111001111110100111101011111011011110111
1111100011111001111110101111101111111100111111011111111011111111
-- Bytes as bits (alt):
□□□□□□□□□□□□□□□■□□□□□□■□□□□□□□■■□□□□□■□□□□□□□■□■□□□□□■■□□□□□□■■■
□□□□■□□□□□□□■□□■□□□□■□■□□□□□■□■■□□□□■■□□□□□□■■□■□□□□■■■□□□□□■■■■
□□□■□□□□□□□■□□□■□□□■□□■□□□□■□□■■□□□■□■□□□□□■□■□■□□□■□■■□□□□■□■■■
□□□■■□□□□□□■■□□■□□□■■□■□□□□■■□■■□□□■■■□□□□□■■■□■□□□■■■■□□□□■■■■■
□□■□□□□□□□■□□□□■□□■□□□■□□□■□□□■■□□■□□■□□□□■□□■□■□□■□□■■□□□■□□■■■
□□■□■□□□□□■□■□□■□□■□■□■□□□■□■□■■□□■□■■□□□□■□■■□■□□■□■■■□□□■□■■■■
□□■■□□□□□□■■□□□■□□■■□□■□□□■■□□■■□□■■□■□□□□■■□■□■□□■■□■■□□□■■□■■■
□□■■■□□□□□■■■□□■□□■■■□■□□□■■■□■■□□■■■■□□□□■■■■□■□□■■■■■□□□■■■■■■
□■□□□□□□□■□□□□□■□■□□□□■□□■□□□□■■□■□□□■□□□■□□□■□■□■□□□■■□□■□□□■■■
□■□□■□□□□■□□■□□■□■□□■□■□□■□□■□■■□■□□■■□□□■□□■■□■□■□□■■■□□■□□■■■■
□■□■□□□□□■□■□□□■□■□■□□■□□■□■□□■■□■□■□■□□□■□■□■□■□■□■□■■□□■□■□■■■
□■□■■□□□□■□■■□□■□■□■■□■□□■□■■□■■□■□■■■□□□■□■■■□■□■□■■■■□□■□■■■■■
□■■□□□□□□■■□□□□■□■■□□□■□□■■□□□■■□■■□□■□□□■■□□■□■□■■□□■■□□■■□□■■■
□■■□■□□□□■■□■□□■□■■□■□■□□■■□■□■■□■■□■■□□□■■□■■□■□■■□■■■□□■■□■■■■
□■■■□□□□□■■■□□□■□■■■□□■□□■■■□□■■□■■■□■□□□■■■□■□■□■■■□■■□□■■■□■■■
□■■■■□□□□■■■■□□■□■■■■□■□□■■■■□■■□■■■■■□□□■■■■■□■□■■■■■■□□■■■■■■■
■□□□□□□□■□□□□□□■■□□□□□■□■□□□□□■■■□□□□■□□■□□□□■□■■□□□□■■□■□□□□■■■
■□□□■□□□■□□□■□□■■□□□■□■□■□□□■□■■■□□□■■□□■□□□■■□■■□□□■■■□■□□□■■■■
■□□■□□□□■□□■□□□■■□□■□□■□■□□■□□■■■□□■□■□□■□□■□■□■■□□■□■■□■□□■□■■■
■□□■■□□□■□□■■□□■■□□■■□■□■□□■■□■■■□□■■■□□■□□■■■□■■□□■■■■□■□□■■■■■
■□■□□□□□■□■□□□□■■□■□□□■□■□■□□□■■■□■□□■□□■□■□□■□■■□■□□■■□■□■□□■■■
■□■□■□□□■□■□■□□■■□■□■□■□■□■□■□■■■□■□■■□□■□■□■■□■■□■□■■■□■□■□■■■■
■□■■□□□□■□■■□□□■■□■■□□■□■□■■□□■■■□■■□■□□■□■■□■□■■□■■□■■□■□■■□■■■
■□■■■□□□■□■■■□□■■□■■■□■□■□■■■□■■■□■■■■□□■□■■■■□■■□■■■■■□■□■■■■■■
■■□□□□□□■■□□□□□■■■□□□□■□■■□□□□■■■■□□□■□□■■□□□■□■■■□□□■■□■■□□□■■■
■■□□■□□□■■□□■□□■■■□□■□■□■■□□■□■■■■□□■■□□■■□□■■□■■■□□■■■□■■□□■■■■
■■□■□□□□■■□■□□□■■■□■□□■□■■□■□□■■■■□■□■□□■■□■□■□■■■□■□■■□■■□■□■■■
■■□■■□□□■■□■■□□■■■□■■□■□■■□■■□■■■■□■■■□□■■□■■■□■■■□■■■■□■■□■■■■■
■■■□□□□□■■■□□□□■■■■□□□■□■■■□□□■■■■■□□■□□■■■□□■□■■■■□□■■□■■■□□■■■
■■■□■□□□■■■□■□□■■■■□■□■□■■■□■□■■■■■□■■□□■■■□■■□■■■■□■■■□■■■□■■■■
■■■■□□□□■■■■□□□■■■■■□□■□■■■■□□■■■■■■□■□□■■■■□■□■■■■■□■■□■■■■□■■■
■■■■■□□□■■■■■□□■■■■■■□■□■■■■■□■■■■■■■■□□■■■■■■□■■■■■■■■□■■■■■■■■
-- Bytes as hex:
000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F
404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F
606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F
808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F
A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF
C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF
E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
-- Bytes as hex (alt):
┼┼┼┽┼┾┼┿┼╀┼╁┼╂┼╃┼╄┼╅┼╆┼╇┼╈┼╉┼╊┼╋┽┼┽┽┽┾┽┿┽╀┽╁┽╂┽╃┽╄┽╅┽╆┽╇┽╈┽╉┽╊┽╋
┾┼┾┽┾┾┾┿┾╀┾╁┾╂┾╃┾╄┾╅┾╆┾╇┾╈┾╉┾╊┾╋┿┼┿┽┿┾┿┿┿╀┿╁┿╂┿╃┿╄┿╅┿╆┿╇┿╈┿╉┿╊┿╋
╀┼╀┽╀┾╀┿╀╀╀╁╀╂╀╃╀╄╀╅╀╆╀╇╀╈╀╉╀╊╀╋╁┼╁┽╁┾╁┿╁╀╁╁╁╂╁╃╁╄╁╅╁╆╁╇╁╈╁╉╁╊╁╋
╂┼╂┽╂┾╂┿╂╀╂╁╂╂╂╃╂╄╂╅╂╆╂╇╂╈╂╉╂╊╂╋╃┼╃┽╃┾╃┿╃╀╃╁╃╂╃╃╃╄╃╅╃╆╃╇╃╈╃╉╃╊╃╋
╄┼╄┽╄┾╄┿╄╀╄╁╄╂╄╃╄╄╄╅╄╆╄╇╄╈╄╉╄╊╄╋╅┼╅┽╅┾╅┿╅╀╅╁╅╂╅╃╅╄╅╅╅╆╅╇╅╈╅╉╅╊╅╋
╆┼╆┽╆┾╆┿╆╀╆╁╆╂╆╃╆╄╆╅╆╆╆╇╆╈╆╉╆╊╆╋╇┼╇┽╇┾╇┿╇╀╇╁╇╂╇╃╇╄╇╅╇╆╇╇╇╈╇╉╇╊╇╋
╈┼╈┽╈┾╈┿╈╀╈╁╈╂╈╃╈╄╈╅╈╆╈╇╈╈╈╉╈╊╈╋╉┼╉┽╉┾╉┿╉╀╉╁╉╂╉╃╉╄╉╅╉╆╉╇╉╈╉╉╉╊╉╋
╊┼╊┽╊┾╊┿╊╀╊╁╊╂╊╃╊╄╊╅╊╆╊╇╊╈╊╉╊╊╊╋╋┼╋┽╋┾╋┿╋╀╋╁╋╂╋╃╋╄╋╅╋╆╋╇╋╈╋╉╋╊╋╋