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

  • Home
  • SEARCH
  • 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 5840251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:42:53+00:00 2026-05-22T11:42:53+00:00

We divided an int to save three values into it. For example the first

  • 0

We divided an int to save three values into it. For example the first 8 bits (from left to right) hold one value, the 8th to 12th bits hold another value and rest of bits hold the third value.

I am writing a utility method to get value from a certain range of bits of an int. is it good enough? do you have a better solution? The startBitPos and endBitPos are count from right to left.

public static int bitsValue(int intNum, int startBitPos, int endBitPos) 
{           
   //parameters checking ignored for now        
    int tempValue = intNum << endBitPos;
    return tempValue >> (startBitPos + endBitPos); 
}

EDIT:

I am sure all values will be unsign.

  • 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-22T11:42:54+00:00Added an answer on May 22, 2026 at 11:42 am

    No, this isn’t quite right at the moment:

    • You should use the unsigned right shift operator to avoid ending up with negative numbers when you don’t want them. (That’s assuming the original values are unsigned, of course.)
    • You’re not shifting left by the appropriate amount to clear the extraneous high bits.

    I suspect you want:

    // Clear unnecessary high bits
    int tempValue = intNum << (31 - endBitPos);
    // Shift back to the lowest bits
    return tempValue >>> (31 - endBitPos + startBitPos);
    

    Personally I’d feel more comfortable with a mask-and-shift than this double shifting, but I’m finding it hard to come up with something as short as the above.

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

Sidebar

Related Questions

My questions are divided into three parts Question 1 Consider the below code, #include
My application is logically divided into server, which contain my Grails domain objects and
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
In my programs infinity usually arises when a value is divided by zero. I
I have a memory block that is divided into a series of location that
I have ID values of the type unsigned int . I need to map
I have a range in java implemented as a class which is divided into
Consider the following signature in C#: double Divide(int numerator, int denominator); Is there a
We are developing automated regression tests using VMWare and NUnit. We have divided tests
All numbers that divide evenly into x. I put in 4 it returns: 4,

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.