This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

how can I assign -1 as Hex Literal to byte type var?  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I want to assigne -128 to the byte type ,I can do it in the following ways

byte b1 = -128; //int but within byte range
byte b2 = -0x80; // hexadecimal for -128
System.out.println("b1 ="+b1+" b2 ="+b2);
output
-128 -128

Now I want to assigne -1 in the same way

byte b1 = -1; //int within byte range
byte b2 = -0xff; // hexadecimal of -1
System.out.println("b1 ="+b1+" b2 ="+b2);

But I m getting error at line
--> byte b2 = -0xff ;
Possible loss of precision...

But If I do explicit cast,It will assigne +1 to b2

How Can I assign -1 in hex to byte ?
Please explain me this concept.

Thanks ...
[ January 27, 2005: Message edited by: Waez Ali ]
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hex literals are int. Therefore, your literal 0xff is really 0x000000ff, which is a positive int. When you apply unary "-" operator to that, you get a negative int (0xffffff01, I think). That negative int is not in byte range.
 
Waez Ali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi peter,

Does that mean i can not assign -1 or -2 as hex literals to byte var?
I am still not clear bkz it allows other -ve numbers like -121 -128 etc.
Can you please elaborate it.

thanks.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully, this clears it up: -

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
It looks like -0xff is a hexadecimal value for - 255.

murali
 
Waez Ali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Thanks for such a good explanation.
You are just great.

Thak you once again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!