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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:24:21+00:00 2026-06-17T16:24:21+00:00

Having trouble writing the bit operations to convert an int to a short depending

  • 0

Having trouble writing the bit operations to convert an int to a short depending on which 16 bits wanted, ex: 1 will be the left most 16 bits and 0 will be the right most 16. All help is appreciated!

    /**
 * Get a short from an int.
 * 
 * Examples: 
 *     getShort(0x56781234, 0); // => 0x1234
 *     getShort(0xFF254545, 1); // => 0xFF25
 * 
 * @param num The int to get a short from.
 * @param which Determines which short gets returned - 0 for least-significant short.
 *            
 * @return A short corresponding to the "which" parameter from num.
 */
public static int getShort(int num, int which)
{

    if(which == 1){
        return num>>16;
    }
    else{
        return num << 16;
    }
    }

I do not want to use >>> or <<<

  • 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-17T16:24:22+00:00Added an answer on June 17, 2026 at 4:24 pm

    This snippet will work correctly with both positive and negative numbers:

    public static int getShort(int num, int which) {
        if(which == 1){
            return num >>> 16; // Use >>> to avoid sign-extending the result
        } else{
            return num & 0xFFFF;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of trouble with a C# program I'm writing and it'd
I'm having a bit of trouble writing a class that uses generics because this
I'm having a bit of trouble writing an sql for this scenario. For example
I'm having some logic trouble with a bit of jQuery I'm writing. I suspect
I'm having a bit of trouble writing my documentation for a set of grouped
I'm writing a currency converter but I'm having a bit of trouble caculating the
I am having a bit of trouble with C++ classes. I am writing a
I am having a bit of trouble writing an android app that can communicate
I'm having trouble writing a Qi grammar which utilizes another Qi grammar. A similar
I am having trouble writing a HQL query which uses a where in clause.

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.