Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
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
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
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
Thanks again.

Pawanpreet Singh
Ranch Hand
Posts: 264
Consider the following code

int n=64;
output is [64/pow(2,5)]= 2

but if n = -64