0

For an assignment I get some bytes, make some calculations on their Binary values and have to return that calculated Stuff in a Byte Array.

My Problem is now, that byte only stores up to 127, but my values can be up to 2^8-1 (11111111). I already tried to convert it to hex, but it´s of course not working either.

So, is that possible, if yes how, or is that exercise not possible like that?

EsoMoa
  • 687
  • 1
  • 8
  • 19

2 Answers2

4

If I'm understanding your question, before you perform any calculations you need to AND (&) your bytes against 255, so you'll be dealing with values from 0 to 255 instead of -128 to 127. When you assign a value higher than 127 to a byte it overflows, so 128 as a byte in java is -128, but after you AND (&) -128 against 255 you'll get +128.

// This prints 0 to 127, then -128 to -1
public static void main(String[] args) {
    for (int i = 0; i <= 255; i++) {
        System.out.println(((byte)i));
    }
}

// Where this will print 0 to 255
public static void main(String[] args) {
    for (int i = 0; i <= 255; i++) {
        System.out.println(((byte)i) & 255);
    }
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • This doesn't really get around the problem, though. In your first version, you're calling `System.out.println(byte)`, and in the second one, `System.out.println(int)`. Importantly, the `&` operation is promoting `i` to `int` because the literal 255 is an `int`. – gobernador Jun 17 '15 at 14:51
  • @gobernador It's an explanation of how bytes are stored from the minimum value to the maximum value. To me it sounds like the OP wants to work with values from 0 - 255, which with a byte can still be done. I'm merely demonstrating how that's done. – Shar1er80 Jun 17 '15 at 14:58
  • Thanks, that´s it. But I don´t get why it´s from 0 to 255 if it AND it with 255. I suppose that a normal byte is up to 01111111, and when I add an 1 it looks like 10000000, which is then -128 ? If I have now 01111111 +1 = 10000000 & FF , wouldn´t it be 11111111? – EsoMoa Jun 17 '15 at 15:57
  • @EsoMoa In Java, the Most Significant Bit (MSB) is the sign bit. It determines if a number of positive or negative. When you AND (&) against 255 (An integer [4 bytes]), as gobemador said, the byte gets promoted to an integer (32 bits where the sign bit is now the 32 bit vs the 8 bit for a byte). So if you have 0111111 + 1 = 10000000 & FF you would get 00000000000000000000000010000000 which is positive 128. – Shar1er80 Jun 17 '15 at 17:43
  • @EsoMoa If you found that my answer helped you solve your issue, kindly click the check mark to my answer so you question is solved. – Shar1er80 Jun 17 '15 at 17:44
  • Yes. of course. But I had to think about your answer. So that means since I use 0xff is bigger than byte it "casts" the intern "Register" ? to an int. – EsoMoa Jun 18 '15 at 23:11
2

Typically, in Java, this number would be represented as an int. That gives you more than enough room to store your values. If you need to represent it as a byte, for transmitting it somewhere or something like that, then you would cast it like

int i = 255;
byte b = (byte) (i & 0xff);

but in this case, it will still be negative, because byte is signed in Java.

Also, hexadecimal literals are by default of type int, as you've probably found out. So 0xff does have the value of 255 like you intend, but it's an int, not a byte.

gobernador
  • 5,659
  • 3
  • 32
  • 51