• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Bitwise OR

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code;

int test = 0;
test |= 0x80;

produces test = 0x80 in the debugger, which is what you would probably expect.

this code however;

int test = 0;
test |= (byte)0x80;

produces test = 0xffffff80

Can anyone explain this? Is there anyway to perform an unsigned OR like we have in C?
 
Keith Tingle
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Corey has already answered this question in a previous post.

The operands of all bitwise operators are implicitly casted to integers before anything happens. So when I cast 0x80 to byte I am actually promoting 0x80 to 0xffffff80 (integer value -128).

In its literal form java does not upcast the 0x80.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Byte in Java is signed value. I think we can do following

test |= (byte) 0x80 & 0xFF; // & 0xFF to make it unsigned byte, then do OR
 
reply
    Bookmark Topic Watch Topic
  • New Topic