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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Applying >> operator

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Consider the following code

int a=5;
System.out.println("a>>3 :"+(a>>3));
System.out.println("a>>>3 :"+(a>>>3));
a=-5;
System.out.println("a>>3 :"+(a>>3));
System.out.println("a>>>3 :"+(a>>>3));

/**
output is :
a>>3 :0
a>>>3 :0
a>>3 :-1
a>>>3 :536870911
*/
Is there any direct and fast way to find the answer of such questions without bit manipulation?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

Try this

Computation of expression n>>s

For nonnegative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s,

and for negative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s and -1,

for example -10>>2 = -3 is compuatated as [ (-10 / (2 * 2) ) - 1 ] = -3
and 10>>2 = 2 is computated as [ 10 / (2 * 2) ] = 2

and Computation of expression n>>>s

The value of n>>>s is n right-shifted s bit positions with zero-extension. If n is positive, then the result is the same as that of n>>s; if n is negative, the result is equal to that of the expression (n>>s)+(2<<~s) if the type of the left-hand operand is int, and to the result of the expression (n>>s)+(2L<<~s) if the type of the left-hand operand is long. The added term (2<<~s) or (2L<<~s) cancels out the propagated sign bit. (Note that, because of the implicit masking of the right-hand operand of a shift operator, ~s as a shift distance is equivalent to 31-s when shifting an int value and to 63-s when shifting a long value.)

Regards,
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks again.
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Consider the following code

int n=64;
int answer = n>>5;
System.out.println(answer);
output is [64/pow(2,5)]= 2

but if n = -64
int answer = n>>5;
System.out.println(answer);
output is [(-64/pow(2,5))-1]= [(-64/32)-1]= -3
but on running output is -2 (not -3)
WHY ??
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The answer to this question can be found here So closing this topic.

Are not the powers of a moderator wonderful?
[ June 16, 2005: Message edited by: Barry Gaunt ]
 
Danger, 10,000 volts, very electic .... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
    Bookmark Topic Watch Topic
  • New Topic