• Post Reply Bookmark Topic Watch Topic
  • New Topic

Operators  RSS feed

 
Aruna Balasuriya
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {

public static void main(String[] args) {

System.out.println(-1>>>1);

}

}

result is 2147483647

How ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bit pattern for an integer with a value of negative one is ...

1111 1111 1111 1111 1111 1111 1111 1111

When you shift it right by one (using >>>), you get ...

0111 1111 1111 1111 1111 1111 1111 1111

And if you work out the math, converting this binary to decimal, you should get the value.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!