Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4041994
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:57:00+00:00 2026-05-20T12:57:00+00:00

I came across the following program and it behaving in unexpected manner. public class

  • 0

I came across the following program and it behaving in unexpected manner.

public class ShiftProgram
{
      public static void main(String[] args)
      {
             int i = 0;
             while(-1 << i != 0)
                   i++;
             System.out.println(i);
      }
}

If we think about this program output, when it reaches 32 while loop condition should return false and terminate and it should print 32.

If you ran this program, it does not print anything but goes into an infinite loop. Any idea whats going on? Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:57:01+00:00Added an answer on May 20, 2026 at 12:57 pm

    Have you tried printing out (-1 << i) in the loop to see what’s going wrong? If you do, you’ll see that it goes:

    -1 << 0 = -1
    -1 << 1 = -2
    -1 << 2 = -4
    -1 << 3 = -8
    -1 << 4 = -16
    -1 << 5 = -32
    -1 << 6 = -64
    -1 << 7 = -128
    -1 << 8 = -256
    -1 << 9 = -512
    -1 << 10 = -1024
    -1 << 11 = -2048
    -1 << 12 = -4096
    -1 << 13 = -8192
    -1 << 14 = -16384
    -1 << 15 = -32768
    -1 << 16 = -65536
    -1 << 17 = -131072
    -1 << 18 = -262144
    -1 << 19 = -524288
    -1 << 20 = -1048576
    -1 << 21 = -2097152
    -1 << 22 = -4194304
    -1 << 23 = -8388608
    -1 << 24 = -16777216
    -1 << 25 = -33554432
    -1 << 26 = -67108864
    -1 << 27 = -134217728
    -1 << 28 = -268435456
    -1 << 29 = -536870912
    -1 << 30 = -1073741824
    -1 << 31 = -2147483648
    -1 << 32 = -1
    -1 << 33 = -2
    -1 << 34 = -4
    -1 << 35 = -8
    -1 << 36 = -16
    [.. etc ..]
    

    According to the language specification:

    The value of n<<s is n left-shifted s bit positions; this is equivalent (even if overflow occurs) to multiplication by two to the power s.

    … so the result will always remain negative.

    That document also tells you that:

    If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

    So if you use a shift of 32, that’s interpreted as a shift of 32 & 0x1f, which is 0. -1 shifted by 0 is still just -1, not 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time that I want to do a Layout, I'm getting a black layout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.