With thanks to Su Young for the binary workings algorithm and Adit for the idea to include a text input.