Pregunta

I'm having trouble with a programming homework problem in assembly. I have to enter 2 numbers into the console to "diffuse a bomb". The only clue I have to figure out what these two numbers are is by reading the assembly code. Within the assembly code, there are 3 criteria that the numbers (let's call them a and b) must satisfy:

  1. a + b = 3997

  2. ((2 * a) & a) & ((2 * a) & a) = 0

  3. (a ^ b) & (a ^ b) <= 0

For clarification, * is the multiplication operator, + is the addition operator, & is the bitwise AND operator, and ^ is the bitwise XOR operator

It's got me stumped. Any insight would be appreciated.

Thanks!

¿Fue útil?

Solución

From point #2: x & x = 0 if and only if x = 0, so (2 * a) & a = 0. Since multiplying by 2 is a single bit shift, we can see that this is true for any number that doesn't have two set bits next to each other.

From point #3: x & x <= 0 if and only if x <= 0, so that means a ^ b <= 0. If a ^ b = 0 that would mean a = b but point #1 doesn't allow that because 3997 is odd. Thus, a ^ b < 0 which means a and b differ in the sign bit, that is one is positive or zero, the other is negative.

These leave us some freedom of choice. For point #2 we can simply pick a power of 2 that is greater than 3997, then calculate the other number by subtracting from 3997. For example, a = 4096, b = -99:

  1. 4096 + (-99) = 3997
  2. ((2 * 4096) & 4096) & ((2 * 4096) & 4096) = 0
  3. (4096 ^ -99) & (4096 ^ -99) = -4195

There are many other solutions.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top