• 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