Valhalla Legends Archive

Programming => General Programming => Java Programming => Topic started by: Orillion on December 01, 2003, 12:55 AM

Title: byte limitations
Post by: Orillion on December 01, 2003, 12:55 AM
After testing iago's binary packet buffer he wrote for his Javabot, it became apparent that adding bytes with high values seemed to be impossible so I ran some quick tests only to discover this error


*** Semantic Error: A byte value must be an integer value in the range of -128..127

NOTE: I achieved this error from just compiling a primitive byte with a value of 255.

From my understanding this means we'd never be able to add a byte with a decimal value higher than 127. This would make adding 0xff for example, impossible. Any thoughts on this problem?
Title: Re:byte limitations
Post by: iago on December 01, 2003, 02:34 AM
Hmm, I think a simple change can fix that.  Just store it as an integer or long until it gets put into a byte, then mask it to put it into the byte.
Title: Re:byte limitations
Post by: Etheran on December 01, 2003, 04:48 PM
Quote from: Orillion on December 01, 2003, 12:55 AM
After testing iago's binary packet buffer he wrote for his Javabot, it became apparent that adding bytes with high values seemed to be impossible so I ran some quick tests only to discover this error


*** Semantic Error: A byte value must be an integer value in the range of -128..127

NOTE: I achieved this error from just compiling a primitive byte with a value of 255.

From my understanding this means we'd never be able to add a byte with a decimal value higher than 127. This would make adding 0xff for example, impossible. Any thoughts on this problem?
why wouldn't 0xff work?  in a signed variable 0xff = -128 am I wrong?
Title: Re:byte limitations
Post by: Skywing on December 01, 2003, 05:33 PM
Quote from: Etheran on December 01, 2003, 04:48 PM
Quote from: Orillion on December 01, 2003, 12:55 AM
After testing iago's binary packet buffer he wrote for his Javabot, it became apparent that adding bytes with high values seemed to be impossible so I ran some quick tests only to discover this error


*** Semantic Error: A byte value must be an integer value in the range of -128..127

NOTE: I achieved this error from just compiling a primitive byte with a value of 255.

From my understanding this means we'd never be able to add a byte with a decimal value higher than 127. This would make adding 0xff for example, impossible. Any thoughts on this problem?
why wouldn't 0xff work?  in a signed variable 0xff = -128 am I wrong?
I think the problem they are speaking of is having to use signed constants (this being inconvenient and a pain).
Title: Re:byte limitations
Post by: Kp on December 01, 2003, 05:54 PM
Quote from: Etheran on December 01, 2003, 04:48 PMin a signed variable 0xff = -128 am I wrong?

Yes.  0xff = 255 = -1.
Title: Re:byte limitations
Post by: Etheran on December 01, 2003, 07:00 PM
Quote from: Kp on December 01, 2003, 05:54 PM
Quote from: Etheran on December 01, 2003, 04:48 PMin a signed variable 0xff = -128 am I wrong?

Yes.  0xff = 255 = -1.
err yes of course, that would be 0x80 = -128 and 0x7F = 127
Title: Re:byte limitations
Post by: Orillion on December 01, 2003, 10:09 PM
Yeah the problem i think stems from the fact that java automatically signs everything. Although we have gotten around that to a certain extent
Title: Re:byte limitations
Post by: j0k3r on December 08, 2003, 06:46 AM
Er, sort of off topic, what part of '0x80' determines that it's negative 128?
Title: Re:byte limitations
Post by: Banana fanna fo fanna on December 08, 2003, 08:15 AM
The sign bit, or the first bit in the number.
Title: Re:byte limitations
Post by: iago on December 08, 2003, 11:37 AM
Quote from: St0rm.iD on December 08, 2003, 08:15 AM
The sign bit, or the first bit in the number.

Technically, it's the last bit.  I would call it the left-most bit :)
Title: Re:byte limitations
Post by: Banana fanna fo fanna on December 08, 2003, 02:13 PM
Yes, left-most.
Title: Re:byte limitations
Post by: Etheran on December 11, 2003, 01:32 PM
and to find your signed value if the sign bit is set, you take the two's complement of the number:
~x + 1

example:

1101 1001 = 0xC9

take the two's complement:

0010 0110 + 1 = 0010 0111 = 39
add the sign and you end up with -39
Title: Re:byte limitations
Post by: iago on January 01, 2004, 06:14 AM
hmm, I don't see what your problem was.  I just tested it out with the following code:

   public static void main(String args[])
   {
       Buffer buf = new Buffer();

       buf.add((byte)250);
       buf.add((byte)251);
       buf.add((byte)252);
       buf.add((byte)253);
       buf.add((byte)254);
       buf.add((byte)255);

       buf.add((byte)-1);
       buf.add((byte)-2);
       buf.add((byte)-3);
       buf.add((byte)-4);
       buf.add((byte)-5);

       System.out.println(buf.toText());
   }


And it came back with the (correct) results:
Buffer contents:
fa fb fc fd fe ff ff fe fd fc fb                        ...........
Length: 11