Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
    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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Applying >> operator

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Report post to moderator
Thanks again.
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Just the other day, I was thinking ... about this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic