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 does everything in pattern space (a la dc.sed).
34 # --------------------------------------------------------------------------
35 # This was actually used in a one-disk distribution of Linux to compute
36 # netmasks as follows (1 parameter => compute netmask e.g. 24 becomes
37 # 255.255.255.0; 2 parameters => given host address and netmask compute
38 # network and broadcast addresses):
41 # OUTPUT='$1.$2.$3.$4'
42 # set 255.255.255.255 $1
44 # OUTPUT='$1.$2.$3.$4 $5.$6.$7.$8'
47 # if [ `expr $2 : ".*\\."` -gt 0 ]; then
48 # MASK="$2 br b8<r b16<r b24< R|R|R|"
51 # 11111111111111111111111111111111 x>1> x<1<"
54 # set `echo "$1 br b8<r b16<r b24< R|R|R| D # Load address
55 # $MASK D ~r # Load mask
57 # & DDD 24>dpP 16>11111111& dpP 8>11111111& dpP 11111111& dpP
58 # | DDD 24>dpP 16>11111111& dpP 8>11111111& dpP 11111111& dpP
59 # " | sed -f binary.sed`
62 # --------------------------------------------------------------------------
68 s/\(.*%%\) *\([0-9][0-9]*\)/\2\
77 /^.*%%D/ s/^[^\n]*\n/&&/
78 /^.*%%P/ s/^[^\n]*\n//
79 /^.*%%x/ s/^\([^\n]*\n\)\([^\n]*\n\)/\2\1/
80 /^.*%%r/ s/^\([^\n]*\n\)\([^%]*\)/\2\1/
81 /^.*%%R/ s/^\([^%]*\n\)\([^\n]*\n\)/\2\1/
99 # Decimal to binary via analog form
100 s/^\([^\n]*\)/-&;9876543210aaaaaaaaa/
102 s/\(a*\)-\(.\)\([^;]*;[0-9]*\2.\{9\}\(a*\)\)/\1\1\1\1\1\1\1\1\1\1\4-\3/
104 s/-;9876543210aaaaaaaaa/;a01!/
106 s/\(a*\)\1\(a\{0,1\}\)\(;\2.\(.\)[^!]*!\)/\1\3\4/
112 # Binary to decimal via analog form
113 s/^\([^\n]*\)/-&;10a/
115 s/\(a*\)-\(.\)\([^;]*;[0-9]*\2.\(a*\)\)/\1\1\4-\3/
117 s/-;10a/;aaaaaaaaa0123456789!/
119 s/\(a*\)\1\1\1\1\1\1\1\1\1\(a\{0,9\}\)\(;\2.\{9\}\(.\)[^!]*!\)/\1\3\4/
126 s/\([^\n]*\)\n\([^\n]*\)/-\1-\2-111 01000/
128 s/\([^-]*\)-\([^-]*\)\([^-]\)-\([^-]*\)\([^-]\)-\([01 ]*\3\5\([01]\)\)/\7\1-\2-\4-\6/
130 s/^0*\([^-]*\)-[^\n]*/\1/
136 s/\([^\n]*\)\n\([^\n]*\)/-\1-\2-000 01101/
142 s/\([^\n]*\)\n\([^\n]*\)/-\1-\2-000 10111/
144 s/\([^-]*\)-\([^-]*\)\([^-]\)-\([^-]*\)\([^-]\)-\([01 ]*\3\5\([01]\)\)/\7\1-\2-\4-\6/
146 s/\([^-]*\)-\([^-]*\)-\([^-]*\)-[^\n]*/\2\3\1/
151 s/^\(.\)\([^\n]*\n\)/\1-010-\2/
153 s/\(.\)-0\{0,1\}\1\(.\)0\{0,1\}-\([01\n]\)/\2\3-010-/
156 # If result is 00001..., \3 does not match (it looks for -10) and we just
157 # remove the table and leading zeros. If result is 0000...0, \3 matches
158 # (it looks for -0), \4 is a zero and we leave a lone zero as top of the
161 s/0*\(1\{0,1\}\)\([^-]*\)-\(\1\(0\)\)\{0,1\}[^-]*-/\4\1\2/
165 # Left shift, convert to analog and add a binary digit for each analog digit
166 s/^\([^\n]*\)/-&;9876543210aaaaaaaaa/
168 s/\(a*\)-\(.\)\([^;]*;[0-9]*\2.\{9\}\(a*\)\)/\1\1\1\1\1\1\1\1\1\1\4-\3/
170 s/^\(a*\)-;9876543210aaaaaaaaa\n\([^\n]*\)/\2\1/
175 # Right shift, convert to analog and remove a binary digit for each analog digit
176 s/^\([^\n]*\)/-&;9876543210aaaaaaaaa/
178 s/\(a*\)-\(.\)\([^;]*;[0-9]*\2.\{9\}\(a*\)\)/\1\1\1\1\1\1\1\1\1\1\4-\3/
180 s/^\(a*\)-;9876543210aaaaaaaaa\n\([^\n]*\)/\2\1/