Solved Bytes and Ints in NBT

Discussion in 'Plugin Development' started by Shadow_tingBRO, Mar 15, 2020.

Thread Status:
Not open for further replies.
  1. This is more towards of a java/ computer science question (excuse If this is the wrong place to post this) but i was going through some of the Minecrafts NBT input and output streams code just so I could get a better understanding of how NBT data is saved because this is useful for saving item data etc.. and I came across where a byte was read from a input stream and then it was combined with a bitwise and, with 0xFF (which is 255) and I am confused by this as I read that this will include the sign of the byte when converting to an int but when I was testing around with this, i tried:
    Code:
    byte b = -100;
    System.out.println(b & 0xFF);
    
    And It printed 156 and Im confused, because from my knowledge If you take into account the sign bit from -100 (which is 1) the full binary for the byte -100 is 11100100. But if you combine that with 0xFF (11111111) in a bitwise and, then you would get the same answer as 11100100.. which should be 228.. but it printed 156? Any help of links which would help would be very much appreciated.
     
  2. Offline

    TheEctoplasm

    Hey,
    to my understanding, it's basically casting it to an integer, but without signing it.
    If you cast byte -100 to int it will be -100 (which has different binary value!),
    but if you use & operator with 0xFF (which is an integer) the extra bit creating the sign in byte type will become extra bit in positive integer, the final value is 156.

    Edit:
    btw the binary representation is as follows:
    -100 is 1001 1100
    converted to int: 0000 0000 0000 0000 0000 0000 1001 1100 = 156
     
    Last edited: Mar 16, 2020
  3. I think Im confused at why -100 is 10011100 binary? The first column is the sign, then the 4th is the 16, 5th is 8ths, 6th is 4s? So 16+8+4?
     
  4. Offline

    CraftCreeper6

    Last edited: Mar 16, 2020
  5. Ohhhhhh Thanks that helped a lot, now I understand!
     
Thread Status:
Not open for further replies.

Share This Page