1 # A kind of clone of dc geared towards binary operations.
6 # b convert decimal to binary
7 # d convert binary to decimal
10 # < shift left binary by decimal number of bits (11 3< gives 11000)
11 # > shift right binary by decimal number of bits (1011 2> gives 10)
12 # & binary AND (between two binary operands)
13 # | binary OR (between two binary operands)
14 # ^ binary XOR (between two binary operands)
15 # ~ binary NOT (between one binary operand)
17 # stack manipulation commands
20 # D duplicate stack top
21 # x exchange top two elements
22 # r rotate stack counter-clockwise (second element becomes first)
23 # R rotate stack clockwise (last element becomes first)
26 # l print stack (stack top is first)
28 # q quit, print stack top if any (cq is quiet quit)
30 # The only shortcoming is that you'd better not attempt conversions of
31 # values above 1000 or so.
33 # This version keeps the stack in hold space and the command in pattern
34 # space; it is the fastest one (though the gap with binary3.sed is small).
35 # --------------------------------------------------------------------------
36 # This was actually used in a one-disk distribution of Linux to compute
37 # netmasks as follows (1 parameter => compute netmask e.g. 24 becomes
38 # 255.255.255.0; 2 parameters => given host address and netmask compute
39 # network and broadcast addresses):
42 # OUTPUT='$1.$2.$3.$4'
43 # set 255.255.255.255 $1
45 # OUTPUT='$1.$2.$3.$4 $5.$6.$7.$8'
48 # if [ `expr $2 : ".*\\."` -gt 0 ]; then
49 # MASK="$2 br b8<r b16<r b24< R|R|R|"
52 # 11111111111111111111111111111111 x>1> x<1<"
55 # set `echo "$1 br b8<r b16<r b24< R|R|R| D # Load address
56 # $MASK D ~r # Load mask
58 # & DDD 24>dpP 16>11111111& dpP 8>11111111& dpP 11111111& dpP
59 # | DDD 24>dpP 16>11111111& dpP 8>11111111& dpP 11111111& dpP
60 # " | sed -f binary.sed`
63 # --------------------------------------------------------------------------
76 s/^[0-9][0-9]* *\([^\n]*\).*/\1/
78 s/^\([0-9][0-9]*\)[^\n]*/\1/
83 /^[^DPxrRcplqbd&|^~<>]/b bad
95 s/^\([^\n]*\n\)\([^\n]*\n\)/\2\1/
99 s/^\([^\n]*\n\)\(.*\)/\2\1/
103 s/^\(.*\n\)\([^\n]*\n\)/\2\1/
127 # Decimal to binary via analog form
129 s/^\([^\n]*\)/-&;9876543210aaaaaaaaa/
131 s/\(a*\)-\(.\)\([^;]*;[0-9]*\2.\{9\}\(a*\)\)/\1\1\1\1\1\1\1\1\1\1\4-\3/
133 s/-;9876543210aaaaaaaaa/;a01!/
135 s/\(a*\)\1\(a\{0,1\}\)\(;\2.\(.\)[^!]*!\)/\1\3\4/
141 # Binary to decimal via analog form
143 s/^\([^\n]*\)/-&;10a/
145 s/\(a*\)-\(.\)\([^;]*;[0-9]*\2.\(a*\)\)/\1\1\4-\3/
147 s/-;10a/;aaaaaaaaa0123456789!/
149 s/\(a*\)\1\1\1\1\1\1\1\1\1\(a\{0,9\}\)\(;\2.\{9\}\(.\)[^!]*!\)/\1\3\4/
157 s/\([^\n]*\)\n\([^\n]*\)/-\1-\2-111 01000/
159 s/\([^-]*\)-\([^-]*\)\([^-]\)-\([^-]*\)\([^-]\)-\([01 ]*\3\5\([01]\)\)/\7\1-\2-\4-\6/
161 s/^0*\([^-]*\)-[^\n]*/\1/
168 s/\([^\n]*\)\n\([^\n]*\)/-\1-\2-000 01101/
175 s/\([^\n]*\)\n\([^\n]*\)/-\1-\2-000 10111/
177 s/\([^-]*\)-\([^-]*\)\([^-]\)-\([^-]*\)\([^-]\)-\([01 ]*\3\5\([01]\)\)/\7\1-\2-\4-\6/
179 s/\([^-]*\)-\([^-]*\)-\([^-]*\)-[^\n]*/\2\3\1/
185 s/^\(.\)\([^\n]*\n\)/\1-010-\2/
187 s/\(.\)-0\{0,1\}\1\(.\)0\{0,1\}-\([01\n]\)/\2\3-010-/
190 # If result is 00001..., \3 does not match (it looks for -10) and we just
191 # remove the table and leading zeros. If result is 0000...0, \3 matches
192 # (it looks for -0), \4 is a zero and we leave a lone zero as top of the
195 s/0*\(1\{0,1\}\)\([^-]*\)-\(\1\(0\)\)\{0,1\}[^-]*-/\4\1\2/
199 # Left shift, convert to analog and add a binary digit for each analog digit
201 s/^\([^\n]*\)/-&;9876543210aaaaaaaaa/
203 s/\(a*\)-\(.\)\([^;]*;[0-9]*\2.\{9\}\(a*\)\)/\1\1\1\1\1\1\1\1\1\1\4-\3/
205 s/^\(a*\)-;9876543210aaaaaaaaa\n\([^\n]*\)/\2\1/
210 # Right shift, convert to analog and remove a binary digit for each analog digit
212 s/^\([^\n]*\)/-&;9876543210aaaaaaaaa/
214 s/\(a*\)-\(.\)\([^;]*;[0-9]*\2.\{9\}\(a*\)\)/\1\1\1\1\1\1\1\1\1\1\4-\3/
216 s/^\(a*\)-;9876543210aaaaaaaaa\n\([^\n]*\)/\2\1/