wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Whats the Output. of this.....? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Whats the Output. of this.....?" Watch "Whats the Output. of this.....?" New topic
Author

Whats the Output. of this.....?

Manoj Mani
Ranch Hand

Joined: Mar 31, 2007
Posts: 65
byte b = -13;
b >>>= 1;
System.out.println(b);


Impossible Is Often Untried.Its Time For Us To Change....
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Shift operator not on the 310-055 exam!

Anyways I think you are going for 1.4.

Here you go!



Let us see what output comes:

(11110011) = -13 (2's complement representation of -13)

-13 >>> 1
(11111001) = -7 (2's complement representation of -7)

So the output is -7


Got it Manoj?

Regards,
cmbhatt
[ April 20, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
John Mathew
Greenhorn

Joined: Apr 17, 2007
Posts: 13
I dont think its the right answer....
>>> is unsigned right hift operator
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi John,


I would like to know the correct answer. How >>> operator works.
Although it is not on the exam (310-055).


>>> Shift right, filling with zeros from left;



Regards,
cmbhatt
Tina Tibrewal
Ranch Hand

Joined: Apr 19, 2007
Posts: 40
byte b = -42; // 1101 0110
int result = b >>> 4; // 268435453

It is instructive to compare the value of the expression (-42 >>> 4) with that of the expression (-42 >> 4), which has the value -3. The byte value for b, after unary numeric promotion to int, is shifted right with zero-fill four places.

b >>> 4
= 1111 1111 1111 1111 1111 1111 1101 0110 >>> 4
= 0000 1111 1111 1111 1111 1111 1111 1101
= 0x0ffffffd
= 268435453


But in the asked question since we are using an assigment operator the casting is done by the operator implicitly so
0111 1111 1111 1111 1111 1111 1111 1001 the higher order bits are dropped
and what results is 1111 1001 which is -7.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whats the Output. of this.....?
 
Similar Threads
Need help on the following question?
Exception Inheritance Hierarchy
test
Add date and revision info to the javadoc when checked in
iterating ArrayList in forEach