
Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
> The operation is *integer* comparison, so you are comparing each byte
> with 0x90 (144), not -0x70.
> Read about "binary numeric promotion" here:http://java.sun.com/docs/books/jls/third_edition/html/expressions.htm...http://j
ava.sun.com/docs/books/jls/third_edition/html/conversions.htm...
On Feb 14, 11:55 pm, Eric Sosman <Eric.Sos...@sun.com> wrote:
> (byte)0x90 == -0x70 == -112 is within the range of byte
> values. (byte)0x90 != 0x90; the first is negative and
> the second is positive.
Thanks, the language specifications helped a lot.
On Feb 15, 12:41 am, "Daniel Pitts" <googlegrou...@coloraura.com>
wrote:
> Like others have said, the comparison first converts to integers.
> 0x90 = 192, your loop ranges from -128 to 126.
[quoted text clipped - 5 lines]
> }
> }
Is the casting of the first operand used to force byte calculations?
I tried println(b) with both ways and got the same result -112.
class Test {
public static void main(String[] args) {
for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) {
if (b == (byte)0x90) {
System.out.println("Printed");
System.out.println(b);
}
}//loop-ends
}
}
Gordon Beaton - 15 Feb 2007 12:14 GMT
> Is the casting of the first operand used to force byte calculations?
No, the comparison is done using ints. Follow the links I posted.
Byte operands are promoted to int before the comparison is made.
> I tried println(b) with both ways and got the same result -112.
What both ways?
I think you'll find that these give different results:
System.out.println(0x90);
System.out.println((byte)0x90);
Casting with (byte)b where b is already a byte doesn't change
anything.
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Rajeev - 15 Feb 2007 12:30 GMT
> > Is the casting of the first operand used to force byte calculations?
>
[quoted text clipped - 13 lines]
> Casting with (byte)b where b is already a byte doesn't change
> anything.
Yes, i went through the link and soon did conclude that my earlier
loop was
undergoing a byte to integer conversion. But the next post i was
referring
to another snip werein it used
if ((byte)b == (byte)0x90)
Thanks again.
On Feb 15, 12:41 am, "Daniel Pitts" <googlegrou...@coloraura.com>
wrote:
> Like others have said, the comparison first converts to integers.
> 0x90 = 192, your loop ranges from -128 to 126.
> Two things to do:
> for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) {
> if ((byte)b == (byte)0x90) {
> System.out.println("Printed");
> }
> }
Chris Uppal - 15 Feb 2007 14:55 GMT
> > > Is the casting of the first operand used to force byte calculations?
[...]
> if ((byte)b == (byte)0x90)
I'm not sure if I've understood what you are asking, but I hope this helps...
What that code will do is
1) Cast b to a byte. Since b is already a byte, this has absolutely no effect
and will be ignored by the compiler.
2) Cast 0x90 to a byte, resulting in the decimal byte value -112. (Actually
that will be done at compile-time rather than at runtime, but it makes no
difference here).
3) Convert /both/ byte values to 32-bit integers.
4) Compare those integers.
There is no way that you can compare two bytes directly in Java -- they are
/always/ promoted to integers before the comparison. (And before all other
arithmetic operations, such as subtraction.)
(BTW, if all this seems somewhat overcomplicated to you, then don't worry -- I
think it's overcomplicated too. The real problem is that in Java (against all
good sense) the 'byte' datatype is signed.)
-- chris
Daniel Pitts - 15 Feb 2007 18:10 GMT
On Feb 15, 6:55 am, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
> > > > Is the casting of the first operand used to force byte calculations?
> [...]
[quoted text clipped - 24 lines]
>
> -- chris
Actually, you missed (twice) his reference to MY post, in which b is
an int.