• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Bitwise OR of signed byte value

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could someone please explain the following statement:

"Loads a value from a byte array and performs a bitwise OR with that value. Values loaded from byte arrays are signed bytes, and thus this may not give the desired results."

 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Little context, please! What is the "desired result"?

The following outputs not "fb", but "fffffffb". is that the point to that quote?
[ November 17, 2005: Message edited by: Jeff Albrechtsen ]
 
Rahul Kakkar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I actually got that from here. Hence was couldn't clarify the "desired result".

So basically you're saying that performing a bitwise OR on a negative value could produce such "undesired results"?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since bitwise OR is a well-defined operation, one has to read something into
the pharse "desired result". If an operation does what it's defined to
do, how can that not be the "desired result"?

What often catches people off guard are two things:
1. byte is signed, so it holds values in the range [-128, +127],
as opposed to being unsigned with a range of [0, +255].
2. When performing an operation, a byte value will be promoted to
at least an int first, and promotion preserves sign.
If you put this two facts together, consider the following.

The top bit is on in mask's value (it has value -128, not +128).
In the expression "value | mask", mask is promoted or widened to an
int with the value -128, which in hex is the 32 bit value 0xffffff80.
So one is OR-ing with the mask 0xffffff80, not 0x00000080. To see
if you get this, see if you can spot the error and fix the code.

[ November 17, 2005: Message edited by: Jeff Albrechtsen ]
 
Rahul Kakkar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should we typecast the constants to byte before performing the left shift?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!