0

Update!!! I'm trying to reverse an bitwise Left-Shift. I have code:

int n1; int n2; int n3; n1 = n1 + 128; n2 = n2 + 128; n3 = n3 + 128; int size = n1 + ((n3 << 8) + n2 << 8); // size = 9999 

How could I get back n1,n2,n3 to given the result 9999?

6
  • 2
    This code doesn't pass compilation, so it doesn't do anything. Commented Jul 15, 2020 at 11:48
  • 1
    there seems to be an open paren ( missing somewhere Commented Jul 15, 2020 at 11:49
  • 2
    @Joni that's one error. Another error is the attempt to assign a byte array to an int variable. Commented Jul 15, 2020 at 11:50
  • 2
    In any case, does this answer your question?stackoverflow.com/questions/141525/… Commented Jul 15, 2020 at 11:50
  • 2
    This is decompiled code, right? I really hope people aren't writing such awful code for real. Commented Jul 15, 2020 at 12:09

2 Answers 2

2

Offsetting by 128 is sometimes used in Java to work around byte being signed. Adding 128 ensures that the result (which will be an int) is non-negative again. As long as it is symmetric with the encoder (the corresponding writeMessage), then it is a way to encode unsigned bytes.

After that, the bytes are reassembled into something bigger. That would not work right with signed bytes. By the way I think a pair of parentheses is missing, and the expression should be n1 + ((n3 << 8) + n2 << 8), or clearer: n1 + (n2 << 8) + (n3 << 16)

An alternative is using byteValue & 0xFF to get rid of the leading ones added by sign-extension. That has the benefit that the raw bytes are used "as themselves", without a strange offset.


The inverse is extracting the bytes and offsetting them by 128 again (adding or subtracting 128 would actually do the same thing, but for symmetry it makes more sense to subtract here), for example:

byte n1 = (byte)((size & 0xFF) - 128); byte n2 = (byte)((size >> 8 & 0xFF) - 128); byte n3 = (byte)((size >> 16 & 0xFF) - 128); 

The & 0xFF operation is not strictly necessary (the final cast to byte gets rid of the high bits), but makes it clear that bytes are being extracted.

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

Comments

1

It's called bit shifting. It's shifting the bits (at the binary level) to the right by 8.

128 << 8 = 32768

Say you have 16, 10000 in binary and shift it left by 2; 16 << 2 = 64 (1000000 binary)

This is common for encryption, data compression, or even dealing with something as simple as color values (when you want separate RGB components of a color represented as a single integer)

In your example, it sounds like multiple values were combined into a single integer to conserve space when sending a packet. Bit shifting is a way or extracting those individual values from a larger number. But that's just a guess.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.