Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Comparing byte values doesn't work

Thread view: 
rajeev.nospam@gmail.com - 14 Feb 2007 18:38 GMT
Hi,
I have a doubt.Why doesn't the following loop work?
Logically,the if statement should have been satisfied atleast once.

class Test {
public static void main(String[] args)  {

   for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) {
    if (b == 0x90)
      System.out.print("Printed");
   }//loop-ends
 }
}

It works with:
if(b==(byte)0x90)
But,
0x90   =  00....0000 1001 0000  +144
(byte)0x90    =  1001 0000   -0x70

-0x70 is well within the byte range.Then what makes it unequal to all
byte values?

Thanks in advance.
Gordon Beaton - 14 Feb 2007 18:52 GMT
> -0x70 is well within the byte range.Then what makes it unequal to
> all byte values?

The operation is *integer* comparison, so you are comparing each byte
with 0x90 (144), not -0x70.

As you've already discovered, you need to cast 0x90 to byte for the
comparison to succeed.

Read about "binary numeric promotion" here:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5198
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#170983

/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 Sreedharan - 15 Feb 2007 09:48 GMT
> 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.
Eric Sosman - 14 Feb 2007 18:55 GMT
rajeev.nospam@gmail.com wrote On 02/14/07 13:38,:
> Hi,
> I have a doubt.Why doesn't the following loop work?
[quoted text clipped - 18 lines]
> -0x70 is well within the byte range.Then what makes it unequal to all
> byte values?

   The range of a byte goes from -128 == -0x80 through
127 == 0x7F.  0x90 == 144 is outside that range: it is
a larger value than any byte can hold, so no byte can
ever be equal to it and the test will never be satisfied.
(byte)0x90 == -0x70 == -112 is within the range of byte
values.  (byte)0x90 != 0x90; the first is negative and
the second is positive.

   Besides which, your loop leaves one value untested.

Signature

Eric.Sosman@sun.com

Daniel Pitts - 14 Feb 2007 19:41 GMT
On Feb 14, 10:38 am, "rajeev.nos...@gmail.com"
<rajeev.nos...@gmail.com> wrote:
> Hi,
> I have a doubt.Why doesn't the following loop work?
[quoted text clipped - 21 lines]
>
> Thanks in advance.

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");
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.