28
4
Write a program/function that takes two integers in the range \$0\$ to \$255\$ inclusive, and returns whether the binary forms of the numbers are exactly one bit different.
For example, \$1\$ and \$0\$ have binary forms 00000001
and 00000000
, which are one bit apart. Similarly, \$152\$ and \$24\$ are 010011000
and 000011000
, so they return true.
However, your code must be pristine, such that if any one bit in your program is flipped, it should throw an error. For example, if your program was the single byte a
(01100001
), then all the 8 possible modified programs:
á ! A q i e c `
must throw an error. Make sure you are modifying by bytes (e.g. the á
up there is actually representing the byte \$225\$, not the actual two byte character á
).
Test cases:
0,1 => Truthy
1,0 => Truthy
152,24 => Truthy
10,10 => Falsey
10,11 => Truthy
11,12 => Falsey
255,0 => Falsey
Rules:
- Provide a testing framework that can verify that your program is properly pristine, since there will be a lot of possible programs (number of bytes*8), or else a complete proof of pristineness.
- Please make sure your program is valid before you post it.
- Output needs to be either truthy/falsey (either way around is fine), or else two distinct non-error values
- Errors can be runtime, compiler, interpreter etc.
7
If anyone's looking for a way to generate all possible variations of their solution, this Japt programme should (someone please double check) do the job: https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=rK5jIKT5VDggrKNay15FpVnDrM1kw8PLo2hFWMPDY2lVILc&input=ImEi
– Shaggy – 2019-04-08T09:57:09.7904
Here's one in Python as well: Try it online!
– TFeld – 2019-04-08T10:02:50.880Functions aren't allowed, since you mentioned program? – Kevin Cruijssen – 2019-04-08T10:32:20.017
5@KevinCruijssen I've specified that function submissions are ok – Jo King – 2019-04-08T10:34:45.097
Not any character B. Only one which has a single bit changed compared to A. – Ven – 2019-04-08T14:10:09.030
As I've understood, yes. – Ven – 2019-04-08T14:13:16.403
4That comment has more
+1
s than the majority of my recent solutions! :\ – Shaggy – 2019-04-11T21:34:40.673