• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Shift Operator

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as of the o/p i am getting = false after compiling.
 
Rohit Nath
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

I got output

true
true
true
false
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rohit Nath
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel/Aum,
Thanks for the valuble information!
 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith

I understand my doubts.

Thanks to all
 
author
Posts: 9000
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and remember sports fans - this is only a 1.4 topic!
 
Yes, my master! Here is the tiny ad you asked for:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic