The bug is that
bin(decimal)[2:]
should be bin(decimal)[2:].rjust(8, '0')
or else the leading zeros are not counted.Personally I would probably have used shift for a simple script like this.