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 7882897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:23:38+00:00 2026-06-03T04:23:38+00:00

Why do the following two operations yield different results in Java for x =

  • 0

Why do the following two operations yield different results in Java for x = 31 or 32 but the same results for x=3?

int x=3;
int b = (int) Math.pow(2,x);
int c = 1<<x;

Results:

x=32: b=2147483647; c=1;
x=31: b=2147483647; c=-2147483648;
x=3:  b=8         ; c=8
  • 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-06-03T04:23:40+00:00Added an answer on June 3, 2026 at 4:23 am

    There are multiple issues at play:

    • An int can only store values between -2147483648 and 2147483647.
    • 1 << x only uses the lowest five bits of x. Thus, 1 << 32 is by definition the same as 1 << 0.
    • Shift operations are performed on the two’s-complement integer representation of the value of the left operand; this explains why 1 << 31 is negative.
    • Math.pow(2, 32) returns a double.
    • (int)(d), where d is a double greater than 2147483647 returns 2147483647 (“the largest representable value of type int“).

    What this interview question does is show that (int)Math.pow(2, x) and 1 << x are not equivalent for values of x outside the 0…30 range.

    P.S. It is perhaps interesting to note that using long in place of int (and 1L in place of 1) would give yet another set of results different from the other two. This holds even if the final results are converted to int.

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

Sidebar

Related Questions

Consider the following two Java files that contain a simplified version of my issue
I have the following two tables: Customer { int Id int Name } Bills
I have the following code, basically it is performing two operations. First one being
How would I have multiple $or operations? So far I've tried the following but
I would like to know what the complexity of the following two operations are.
I have the following declaration to distinct that it uses different operations in my
I would like to know which of two following operations under indexed varchar field
Consider the following two blocks of Java psedo-code in a system that uses optimistic
I have the following two Vector objects in java. Vector<SomeClass> obj1; Vector<SomeClass> obj2; The
Consider the two following programs: program one int main() { printf( hello\n ); }

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.