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 / November 2007

Tip: Looking for answers? Try searching our database.

byte arythmetics

Thread view: 
Philipp - 20 Nov 2007 10:22 GMT
Hello,

I can't understand how the following code works.
If I have a signed byte = -1 (1111 1111) and I logical-AND with 0xFF
(1111 1111) how come there is any effect?

Thanks Phil

--- SSCCE ---

public class byteTest {
  public static void main(String[] args) {
    byte a = -1;
       
    System.out.println(a);
    System.out.println((a & 0xFF));
    // Output:
    // -1
    // 255
  }
}
Chris Dollin - 20 Nov 2007 10:45 GMT
> I can't understand how the following code works.
> If I have a signed byte = -1 (1111 1111) and I logical-AND with 0xFF
[quoted text clipped - 15 lines]
>    }
> }

There's no byte arithmetic: the operands are promoted to `int` if
necessary. So the 0xff value that's left is an int value -- 255.

Signature

Chris "congratulations on your promotion, Short!" Dollin

Hewlett-Packard Limited                                          registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN          690597 England

Patricia Shanahan - 20 Nov 2007 12:30 GMT
> Hello,
>
[quoted text clipped - 17 lines]
>   }
> }

a & 0xFF is an int expression, computed by converting a to int, then
doing the operation in 32 bit int arithmetic.

0xFF is int so it would be done that way even if Java supported byte
arithmetic. However, even if you add two byte expressions, it is done by
converting both to int and the result is int.

See the JLS, "5.6.2 Binary Numeric Promotion",
http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#170983

Patricia
Philipp - 20 Nov 2007 16:45 GMT
> See the JLS, "5.6.2 Binary Numeric Promotion",
> http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#170983 

Thanks...
Phil


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.