• 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

doubts on primitives

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)
if int x = 2147483647; I get x>>31 = 0. I understood this.
But how I get x>>32 = 2147483647,x>>33=107374182... so on?
I thoght for that also I should get zero.
2)
why Math.abs(Long.MIN_VALUE) also is negetive?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. if you shift an integer value the right-hand side is first evaluated mod 32 and then the expression is computed
int x = 2147483647;
x >> 32 is equivalent to x >> 0 and thus you get 2147483647;
the same, x >> 33 is equivalent to x >> 1 !
2. From the API


public static long abs(long a)
...
Note that if the argument is equal to the value of Long.MIN_VALUE, the most negative representable long value, the result is that same value, which is negative.


HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited October 23, 2001).]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
1. if you shift an integer value the right-hand side is first evaluated mod 32


Ol' timer Josh Bloch taught me a while ago that technically, the right-hand operand is masked with 0x1F (effectively retaining only the 5 least significant (rightmost) bits) For positive numbers, the effect is the same as mod but for negative numbers, mod and masking have different results.
HTH


------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
this is supposed to be a surprise, but it smells like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic