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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:43:57+00:00 2026-06-09T13:43:57+00:00

Given offset OFF and length LEN, isolate a group of bits and shift it

  • 0

Given offset OFF and length LEN, isolate a group of bits and shift it to the right. (Usage: int holding several smaller-range integers with given offsets and lengths). For example using offset 4 and length 4,

  a = 110101011000
      ----^^^^---- this is the group 
      000000000101
              ^^^^ isolated and right-shifted here

I currently use

(a>>OFF)&((1<<(LEN+1))-1)

giving for the example above

a                         110101011000
a>>OFF                    000011010101
1<<(LEN+1)                000000010000
1<<(LEN+1)-1              000000001111
(a>>OFF)&((1<<(LEN+1))-1) 000000000101

Is there a more readable/efficient way?

  • 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-09T13:43:59+00:00Added an answer on June 9, 2026 at 1:43 pm

    There isn’t a single correct answer in this case. What you did is fine – it’s correct, and with some documentation it’s also pretty clear.

    If you want different ways, you can: try shifting a left and then right again (assuming a is unsigned – otherwise cast it first); or you can first create a mask (in your case: 000011110000), bitwise-and, and only then shift. However, these won’t necessarily be prettier than what you already have.

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

Sidebar

Related Questions

how do i copy memory starting from a given offset. For example int main()
I am am given the following: The offset of the user's time from GMT
I am trying to get the offset hours from UTC, given a summer date.
I need to calculate the offset, in hours, of a given timezone from UTC
Given the following x86 assembly instructions: mov esi, offset off_A cmp esi, offset off_B
bind () needs a length of the sockaddr structure you've given it. Now, for
Is there a way to get the offset time zone for a given date.
I want to convert the timestamp object(fetched from models) according to given UTC offset
Given that the web application doesn't have su privileges, I'd like to execute a
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;

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.