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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:43:24+00:00 2026-05-11T23:43:24+00:00

Hmmm. Consider this program, whose goal is to figure out the best way to

  • 0

Hmmm. Consider this program, whose goal is to figure out the best way to get the bottom 16 bits of an integer, as a signed integer.

public class SignExtend16 {
    public static int get16Bits(int x)
    { 
        return (x & 0xffff) - ((x & 0x8000) << 1);
    }

    public static int get16Bits0(int x)
    { 
        return (int)(short)(x);
    }    

    public static void main(String[] args)
    {
        for (String s : args)
        {
            int x = Integer.parseInt(s);
            System.out.printf("%08x => %08x, %08x\n",
               x, get16Bits0(x), get16Bits(x));
        }
    }
}

I was going to use the code in get16Bits0, but I got the warning “Unnecessary cast from short to int” with Eclipse’s compiler and it makes me suspicious, thinking the compiler could optimize out my “unnecessary cast”. When I run it on my machine using Eclipse’s compiler, I get identical results from both functions that behave the way I intended (test arguments: 1 1111 11111 33333 66666 99999 ). Does this mean I can use get16Bits0? (with suitable warnings to future code maintainers) I’ve always assumed that JRE and compiler behavior are both machine-independent for arithmetic, but this case is testing my faith somewhat.

  • 1 1 Answer
  • 1 View
  • 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-11T23:43:24+00:00Added an answer on May 11, 2026 at 11:43 pm

    Since numeric casts are implicit-friendly, I think the only reason you’re getting the warning is that the compiler will always make the cast to int upon return, making your explicit cast redundant.

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

Sidebar

Related Questions

Hmmm... I have read about this issue on various other threads here on stackoverflow
Hmmm. Okay after revisiting PInvoke, I'm sure that I don't quite get it :-/
I was working on a program,that I need to support new additions. Hmmm. Let
After following the advice in this question successfully, I added a couple additional lines
Hmmm... the Java Iterator<T> has a remove() method but not a replace(T replacement) method.
Hmmm... I'm working at creating an AS3 UI library, since most of the ones
Hmmm...strange problems... I'm making a form to upload syllabi. Basically, someone go search for
Well, there are other ways (hmmm... or rather working ways) to do it, but
I recently made the statement to a colleague that: NullReferenceExceptions should never be explicitly

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.