Valhalla Legends Forums Archive | Java Programming | byte limitations

AuthorMessageTime
Orillion
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

[code]
*** Semantic Error: A byte value must be an integer value in the range of -128..127
[/code]
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?
December 1, 2003, 6:55 AM
iago
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.
December 1, 2003, 8:34 AM
Etheran
[quote author=Orillion link=board=34;threadid=3987;start=0#msg32853 date=1070261736]
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

[code]
*** Semantic Error: A byte value must be an integer value in the range of -128..127
[/code]
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?
[/quote]
why wouldn't 0xff work? in a signed variable 0xff = -128 am I wrong?
December 1, 2003, 10:48 PM
Skywing
[quote author=Etheran link=board=34;threadid=3987;start=0#msg33015 date=1070318923]
[quote author=Orillion link=board=34;threadid=3987;start=0#msg32853 date=1070261736]
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

[code]
*** Semantic Error: A byte value must be an integer value in the range of -128..127
[/code]
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?
[/quote]
why wouldn't 0xff work? in a signed variable 0xff = -128 am I wrong?
[/quote]
I think the problem they are speaking of is having to use signed constants (this being inconvenient and a pain).
December 1, 2003, 11:33 PM
Kp
[quote author=Etheran link=board=34;threadid=3987;start=0#msg33015 date=1070318923]in a signed variable 0xff = -128 am I wrong?[/quote]

Yes. 0xff = 255 = -1.
December 1, 2003, 11:54 PM
Etheran
[quote author=Kp link=board=34;threadid=3987;start=0#msg33044 date=1070322875]
[quote author=Etheran link=board=34;threadid=3987;start=0#msg33015 date=1070318923]in a signed variable 0xff = -128 am I wrong?[/quote]

Yes. 0xff = 255 = -1.
[/quote]err yes of course, that would be 0x80 = -128 and 0x7F = 127
December 2, 2003, 1:00 AM
Orillion
Yeah the problem i think stems from the fact that java automatically signs everything. Although we have gotten around that to a certain extent
December 2, 2003, 4:09 AM
j0k3r
Er, sort of off topic, what part of '0x80' determines that it's negative 128?
December 8, 2003, 12:46 PM
St0rm.iD
The sign bit, or the first bit in the number.
December 8, 2003, 2:15 PM
iago
[quote author=St0rm.iD link=board=34;threadid=3987;start=0#msg34259 date=1070892958]
The sign bit, or the first bit in the number.
[/quote]

Technically, it's the last bit. I would call it the left-most bit :)
December 8, 2003, 5:37 PM
St0rm.iD
Yes, left-most.
December 8, 2003, 8:13 PM
Etheran
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
December 11, 2003, 7:32 PM
iago
hmm, I don't see what your problem was. I just tested it out with the following code:

[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());
}
[/code]

And it came back with the (correct) results:
[code]Buffer contents:
fa fb fc fd fe ff ff fe fd fc fb ...........
Length: 11
[/code]
January 1, 2004, 12:14 PM

Search