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

shift operator formulae

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With out converting integers into binary can we use the shift operator evalutions using the formulas below always? or better convert into binary and use the shift operators. Coverting into binary is time consuming I guess in the exam.

x >>y = (int) x/2^y (if x is greater than y else always zero)
-x >>y = (int) -x/2^y (if x is greater than y else always -1)
x << y = x * 2^y
-x << y = -x * 2^y
x >>>y = x/2^y

If so, what if y is negative? and (-x >>>y)?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For type int, only the last 5 bits of the right operand's value is used. Therefore, the amount of an int shift will always be between 0 and 31, even if the right operand is negative. For type long, the last 6 bits are used, so a long shift is always between 0 and 63.

For x>>>y, if x is positive, then the result is the same as x>>y. But if x is negative, then the result is (x>>y) + (2<<~y).

See section 15.19 of the Java Language Specification:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121
[ March 30, 2005: Message edited by: marc weber ]
 
gayathri mukkavilli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it mean the result of int shift is always between 0 and 31 and the result of long shift is always between 0 and 63?
if so, -12 >>> 8 is giving as 16777215.

I didn't get your point. Can you explain this more clearly?

Thank you very much for your support
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gayathri mukkavilli:
Does it mean the result of int shift is always between 0 and 31 and the result of long shift is always between 0 and 63? ...


It's the amount of the shift -- not the result of the operation -- that's within these bounds.

For example, consider 100>>35. Here the right operand is 35. But since these are ints, only the last five bits of the 35 will be used, so instead of binary 100011, we have binary 11, which is 3. So 100>>35 is equivalent to 100>>3.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you should mug it like this ...
And one more thing shift operators no body use in projects ...
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're best off converting to binary.

For a quick way to do this, see the following thread:
https://coderanch.com/t/248524/java-programmer-SCJP/certification/Bits-Conversion
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic