• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

shift operator

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
can any one explain the step wise calculation for this.I don't understand why the result is -1.
--------------------------------------------
class J {
public static void main (String[] s) {
byte b = 0;
b += ~b >>> 1;
System.out.println(b);
}
}
---------------------------------------------
Thank u.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I imagine this code is intended to test your understanding of signed 2's-complement behavior as well as operator precedence.
Which comes first the unsigned shift operator or the bitwise operator? It's not too baffling, really -- just find the table. Or, just demonstrate it to yourself:

The real test of this code, in my opinion, is to see how quickly you'll think to write your own demonstration.
Elsewhere in another forum a poster was commenting that it seemed silly to test an SCJP with a technical interview. I couldn't disagree more. If I have learned anything from this site, it is that some people are astoundingly good test takers. What I want to know when I hire someone, however, is not whether they're proficient in the elements of the language but how they work. I'd sooner hire someone who can show their work than someone who can rattle everything off from memory.
Do the work! It seems soooo slow to do things this way but you'll get faster. Write code any time you have a doubt; you'll learn far more.
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Basanti Mathad:
Hey,
can any one explain the step wise calculation for this.I don't understand why the result is -1.
--------------------------------------------
class J {
public static void main (String[] s) {
byte b = 0;
b += ~b >>> 1;
System.out.println(b);
}
}
---------------------------------------------
Thank u.



byte b = 0;
b = 00000000;
~b = 11111111;
~b >>> 1 = 11111111; (here, ~b is promoted to int first, then truncated to byte)
b += ~b >>> 1;
b = 11111111 ;
b = -1; (here, byte range is from -2^7 to 2^7 - 1, 11111111 add up to 2^8 - 1, so must be '-')
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
 
sam huang
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think:
~b>>>1 = 2147483647 (01111111111111111111111111111111)
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u all guys........
i was confused bcoz it had assignment operator.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can explain it in this way:
2's complent of x means 1's complement + 1 i.e.
2's comp of x = ~x + 1
Again, a -x is nothing but 2's complement of x.
So, -x = ~x + 1
Thus, remember ~x = -x - 1
Now bit pattern of 0 is nothing but a string of 0s -> 000...000
So, bit pattern of ~0 will be surely a string of 1s -> 111...111
Signed right shift operator >>> makes no change to this pattern 111...111
because, this operator fills the left bits(s) with the left most bit of the
original pattern (1 in this case). So, this is also as same as ~0 which is
nothing but -0 - 1 = -1 (as stated before. -1 added to 0 becomes -1.
Please specify if I am wrong.
 
reply
    Bookmark Topic Watch Topic
  • New Topic