• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question on shift operator

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of the following code block?
public class test {
public static void main(String args[]) {
int i = -1;
i = i >> 1;
System.out.println(i);
}
}

i believe it is 0. it is a question from a site. it says the answer as -1. i doubt it. wat u say?
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in java negative no represents with 2's complment form which means-

int -1-> java representation 0 001->1's compliment 1 110->2's compliment 1111 now this no will get rightshifted->0111->to get back apply rverse process i.e->1's compliment 1 000->2's compliment 1 001

in this no high order bit is 1 hence no will negative & no is 001 i.e->-1.

Hope will understand this!!

Sachin Dimble.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check for 2's complemen calculation link :
https://coderanch.com/t/251801/java-programmer-SCJP/certification/hexadecimal-format

There is a little trick to calculate faster in the exam for -ve numbers:
If it's a odd number let's say (2n + 1) format, then add 1 and devide by 2.. ie your answer in -ve.

ie to get -15 >> 1, answer is -(15+1)/2 = -8
Similarly -16 >> 1, answer is 16/2=8.

Guys, thank me for saving ur binary calculation time
[ December 08, 2005: Message edited by: Enge Chall ]
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>

As the bits are shifted right, the sign bit(left most) is used to fill in from the left. So if the left hand operand is a positive value, zeros are filled in from the left, but if the operand is a negative value, ones are filled in from the left.



public class Test {
public static void main(String args[]) {
int i = -1;
System.out.println( Integer.toBinaryString(i));
System.out.println( Integer.toBinaryString(i>>1));
i = i >> 1;
System.out.println(i);
}
}

Hope this helps u
Regards
Naresh
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic