Wednesday, January 8, 2014

Bit Flipping for binary strings

I ran in to a case where I needed to flip every bit in a very large binary string. I was searching a lot of places and didn't really find anything easy. Fortunately I was way over thinking it. I decided to just use 3 sed statements.

sed -e 's/0/2/g' -e 's/1/0/g' -e 's/2/1/g'

On that same note, if you have messed up spacing in a binary file for whatever reasons, here is a way to fix the spacing. First sed removes all spaces, second one fixes it so there are 8 digit groupings of the binary.

sed 's/[ ]\+//g' | sed 's/\([01][01][01][01][01][01][01][01]\)\([01][01][01][01][01][01][01][01]\)/\1 \2 /g'

No comments:

Post a Comment