*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes shift operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "shift operators" Watch "shift operators" New topic
Author

shift operators

anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hi ranchers,
In dan's operators topic exam i came through this questions,
byte b=0;
b+=~b>>>1;
System.out.println(b);
the ans is -1
the exp is b=b+~b>>>1;
==> b=0+ -1>>>1;
so the result is -1.
but if i compile sepreately -1>>>1,the result is
2147483647,why is that?
anybody could ans me,
yhx


anushree
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by anushree ari:
...but if i compile sepreately -1>>>1,the result is
2147483647,why is that?

Yes, but if you cast that result to a byte then the result will be -1.
The compound assignment operator casts the result to the type of the left hand operand. If all of the 8 least significant bits are set to one then the result of a cast to a primitive byte is -1.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
thx dan,i got it,
 
 
subject: shift operators
 
Similar Threads
Shift operators
right bit shift operator
Please check this qstn from Khalid Mock Exam.
iterating with a scriptlet, can't get the values into js arr
Precedence and associativity of post and pre fix operators