1

Im trying to understand the following piece of code:

int omgezetteTijd =
((0xFF & rekenOmNaarTijdArray[0]) << 24) | ((0xFF & rekenOmNaarTijdArray[1]) << 16) |((0xFF & rekenOmNaarTijdArray[2]) << 8) | (0xFF & rekenOmNaarTijdArray[3]);

What I do not understand is why do you AND it with OxFF, you're ANDING an 8 bit value with 8 bits like so (11111111), so this should give you the same result.

But, when I do not AND it with OxFF I'm getting negative values? Cant figure out why this is happening?

2
  • Why do you repliate the job of what a ByteBuffer can do for you? Commented Jan 22, 2015 at 20:29
  • Because I want to understand different data type's and know how to use them :) Commented Jan 22, 2015 at 20:35

2 Answers 2

5

When you or a byte with an int the byte will be promoted to an int. By default this is done with sign extension. In other words:

                           //                      sign bit
                           //                         v
byte b = -1;               //                         11111111 = -1
int i = (int) b;           // 11111111111111111111111111111111 = -1
                           // \______________________/
                           //      sign extension

By doing & 0xFF you prevent this. I.e.

                           //                      sign bit
                           //                         v
byte b = -1;               //                         11111111 = -1
int i = (int) (0xFF & b);  // 00000000000000000000000011111111 = 255
                           // \______________________/
                           //     no sign extension
Sign up to request clarification or add additional context in comments.

Comments

3

0xFF as a byte represents the number -1. When it's converted to an int, it is still -1, but that has the bit representation 0xFFFFFFFF, because of sign extension. & 0xFF avoids this, treating the byte as unsigned when converting to an int.

Comments

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.