aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift Operator" Watch "Shift Operator" New topic
Author

Shift Operator

Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
what will be the output of this code .

public class ObjectTest {

public static void main(String[] args) {
System.out.println(0 < 1 << 2 >> 1 >>> 2);
}
}

Thanks
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
as of the o/p i am getting = false after compiling.


R.N
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387

Even I am trying to understand the working..
I tried this..

I got output

true
true
true
false
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Sujittt Tripathyrr:
what will be the output of this code .

public class ObjectTest {

public static void main(String[] args) {
System.out.println(0 < 1 << 2 >> 1 >>> 2);
}
}

Thanks


Note that the shift operators have precedence over the relational operators, so the type of the result is going to be a boolean.

1 << 2 = 4 >> 1 = 2 >>> 2 = 0

So the expression boils down to


so it prints false.
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
Can you please tell me difference between >> and >>>

>>
op1 >> op2 Shifts bits of op1 right by distance op2; fills with highest (sign) bit on the left side
>>>
op1 >>> op2 Shifts bits of op1 right by distance op2; fills with 0 bits on the left side

What does the bold lettered line mean? "fills with highest (sign) bit"

Thx in advance.
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
This essentially boils down to the >> operator leaving the sign of the value shifted the same, i.e. a negative number remains a negative number, and a positive number positive, because the space created at the left of the number after the shift will be filled by the value previously at that location (1 if it was negative, 0 if it was positive) and the >>> operator always making the number positive (by filling the shifted space with a 0)

For example in binary

if we >> shift 1101 by 1 we get 1110
if we >>> shift 1101 by 1 we get 0110

Notice how the sign bit on the second example has altered from 1 to 0.

If you want more information on binary numbers (specifically negative number have a look at Wikipedia here)

Daniel


SCJP 1.4,<br />SCJD (URLyBird 1.2.1, JDK 5.0)
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Originally posted by Rohit Kharat:
Can you please tell me difference between >> and >>>

>>
op1 >> op2 Shifts bits of op1 right by distance op2; fills with highest (sign) bit on the left side
>>>
op1 >>> op2 Shifts bits of op1 right by distance op2; fills with 0 bits on the left side

What does the bold lettered line mean? "fills with highest (sign) bit"

Thx in advance.


Here, "fills with highest(sign) bit" refers to the most significant bit(MSB)(i.e. the left most bit). Both the >> and >>> yields the same result for positive numbers, since the MSB is 0. However, when they are applied to negative numbers, their behavior is completely different.

With >>, the sign bit(i.e. 1) is inserted at the MSB.
With >>>, a 0 is inserted at the MSB. So, most often, you always get a positive number.


SCJP 1.4 85%
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
Daniel/Aum,
Thanks for the valuble information!
Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
Thanks Keith

I understand my doubts.

Thanks to all
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
and remember sports fans - this is only a 1.4 topic!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shift Operator