This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Shift operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shift operators " Watch "Shift operators " New topic

Shift operators

Parag Kale

Joined: Apr 08, 2001
Posts: 25
What will be the output of the following ?

The answer is given as -1. How is that ?
(edited by Cindy to complete the formatting)

[This message has been edited by Cindy Glass (edited April 12, 2001).]
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

This is because an int is a 32-bit value, and because the >> operator propegates the high-order bit as it shifts back. Here is a breakdown of what is happening.


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Parag Kale

Joined: Apr 08, 2001
Posts: 25
Ok. Got it now.
subject: Shift operators
It's not a secret anymore!