1

I have two numbers:

num1 = 1
num2 = 1

And then someone applied a secret operation on them:

mask = 2**8 -1
secret = (num1 << 8) | (num2 & mask)
>>> 257

How can I do the opposite of the secret operation? to get from 257 to 1 1

2
  • 2
    You can't recover the original value of of num2 if it is greater than 255, as the mask will erase the high bits in the original. Commented Nov 13, 2021 at 21:57
  • What have you tried / researched already? Commented Nov 13, 2021 at 21:57

1 Answer 1

2

this is a standard way of encoding 2 small integers into a larger one

its 16bits of data representing 2 8 bit integers

to get the first one (upper 8 bits) back you shift it the other way

256 >> 8

to get the lower 8 bits just and it with the original mask

256 & (2**8-1)

as mentioned in the comments most ints are int32 (32 bits, not 8) and you likely cannot recover num2 if its value was greater than 255, because 8 bits can only represent 0-255

as an aside I imagine most people would use (1<<8)-1 instead of 2**8-1

Sign up to request clarification or add additional context in comments.

1 Comment

You shouldn't have done this guy's homework. :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.