Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Operators  RSS feed

 
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 ?
 
author
Marshal
Posts: 23438
138
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!