Skip to content

Instantly share code, notes, and snippets.

@ap
Created November 2, 2009 05:17
Show Gist options
  • Save ap/223957 to your computer and use it in GitHub Desktop.
Save ap/223957 to your computer and use it in GitHub Desktop.

Both of these sed programs transform lines containing binary addition expressions of the form

1+1111111111111

to the respective value of the expression, again in binary.

The easiest way to invoke them in shell is using here strings, like so:

./bin_add.sed <<< 1+1111111111111
./bin_add_fast.sed <<< 1+1111111111111
#!/usr/bin/sed -f
:a
s/\([01]\)$/\1d/
td
s/+$//
b
:d
s/1d/0/
ts1;:s1
s/0d/d1/
td
s/+0*/c+/
:c
s/0c/1/
s/^c/1/
ts2;:s2
s/1c/c0/
tc
ba
#!/usr/bin/sed -f
s/+/C+/
ts1;:s1
:a
s/0$//
tn
s/1$//
ti
s/C//
s/+//
s/^0*//
b
:i
s/C/cC/
:c
s/0c/1/
s/^c/1/
ts2;:s2
s/1c/c0/
tc
:n
s/\(.\)C/C\1/
ta
s/^C/C0/
ts3;:s3
ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment