This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why the ip address value is and with int 255

 
Apoorva Dubey
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have a simple program to find my ip address and converting it to String.
I am unable to understand why the `b[i]` value is and with `(int)255` below.

 
Luke Leber
Ranch Hand
Posts: 69
2
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're working with a byte array (whose integral elements range from -128 to 127) IP address octets are generally represented as unsigned values (no negatives).

In order to properly 'unsign' a byte, you can apply a bitwise operation to it that goes hand in hand with the implicit widening of the data-type to 'int'.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic