• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bitwise operators issue

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could some one explain this behavior while executing this method?

I called this method with:

byte b1 = (byte)-42;
byte b2 = (byte)-89;

public static char getCharFrom2Bytes(byte b1,byte b2)
{
char c = 0;
char c1 = (char)b1;
char c2 = (char)b2;
c = (char)(c | c1);

System.out.println("c | b1-->"+(short)c);//Output here is ok = -42 (0000 0000 1101 0110)

c = (char)(c << 8);

System.out.println("c << 8-->"+(short)c);//Output here is ok = -10752 (1101 0110 0000 0000)

c = (char)(c | c2);

System.out.println("c | b2-->"+(short)c);//Output here is WRONG! = -89 (0000 0000 1010 0111)

return c;
}

Thanks a lot for your time.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you operate on multiple integral values, they're all promoted to the same type -- usually int -- before the operations. So when you try to combine c and c2, you're actually combining (int) c and (int) c2. c2 is sign-extended with a prefix of all ones, not all zeros as you've shown.

If you want to work with just an 8-bit value, then you need to mask off just those bits -- e.g.,

c = (char) (c | (0xff & c2));
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

c = (char)(c | c2);

System.out.println("c | b2-->"+(short)c);


You're not printing what you're calculating.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Jorge Elosegui
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:When you operate on multiple integral values, they're all promoted to the same type -- usually int -- before the operations. So when you try to combine c and c2, you're actually combining (int) c and (int) c2. c2 is sign-extended with a prefix of all ones, not all zeros as you've shown.

If you want to work with just an 8-bit value, then you need to mask off just those bits -- e.g.,

c = (char) (c | (0xff & c2));



Wow...exactly....I had forgotten the 2's complement detail on negative numbers. Wow...it has been like 7 years since the last time I had to deal with bits.

Thanks a million.
 
Jorge Elosegui
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:

c = (char)(c | c2);

System.out.println("c | b2-->"+(short)c);


You're not printing what you're calculating.



I just wanted to see something meaningful. That's why I printed them out as "shorts". Shorts and chars are both 16 bits, so for my purpose they are equivalent. As a "char" I would get ascii weird symbols and I prefer numbers.

The problem was that I forgot to deal with the 2's complement detail on negatives.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't mean the cast to short - I meant the c2 in the assignment and the b2 in the printout.
 
Jorge Elosegui
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:I didn't mean the cast to short - I meant the c2 in the assignment and the b2 in the printout.



Oh, the b2 in the printout is part of a comment. Just to know what I am printing. Notice that it is enclosed in "".

Thanks for your time.

 
reply
    Bookmark Topic Watch Topic
  • New Topic