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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:04:44+00:00 2026-06-12T19:04:44+00:00

Please take a look at my function: int getByte(int x, int n) { int

  • 0

Please take a look at my function:

int getByte(int x, int n) {
   int oneOnes = 255 << ( n << 3);
   int compute = oneOnes & x;
   //FIND A WAY TO RETURN CHAR (NOT INT)
   char result = (compute >> (n << 3));
   return result;
}

Everthing works great until the comment. That is, I start with an integer x, and I want to take only a certain subsection (specified by n). So what I did was make everything except the 8 bits I want to keep into zeros. So for example, if the input was:

 1001011 10011011 00101011 01001011

And I want to keep only the 3rd group of bits (counting from the right), then the result would be:

 00000000 10011011 00000000 00000000 

So I’ve managed to do that correctly. The issue is, I need to return only the bits that I want (with the zeros cropped, as a char). Despite creating a char result and returning that, what’s being returned is still the 32 bit value.

Any help? Thanks!

To be clear: For 00000000 10011011 00000000 00000000, I want only 10011011 to be returned.

Thanks!

  • 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-12T19:04:46+00:00Added an answer on June 12, 2026 at 7:04 pm

    The basic problem is that you’re trying to use signed integers to do this, but shifts of signed integers are not well defined — whenever the bit pattern happens to be a negative value, bad things happen.

    Instead, as is usually the case when doing bit manipulations, you want to use unsigned integers:

    unsigned int getByte(unsigned int x, unsigned int n) {
      unsigned int oneOnes = 255U << ( n << 3);
      unsigned int compute = oneOnes & x;
      return (compute >> (n << 3));
    }
    

    Its even easier if you do the masking AFTER the shifting, as then you don’t need to shift the mask:

    unsigned int getByte(unsigned int x, unsigned int n) {
      return (x >> (n << 3)) & 255U;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at the following autocomplete code. $(document).ready(function(){ $(#the_input).autocomplete(/autocomplete.php, { //some options
Take a look to this function please function CheckHost() { $url = parse_url($_SERVER['HTTP_REFERER']); $host
please take a quick look at this function that I have found on the
I have a question here, please take a look at the link autocorrelation function
Can someone please take a look at these, it just is not happing for
Please take a look at this code: class Foo { public $barInstance; public function
Please take a look at the following: jQuery.fn.jqPos = function(target, settings) { settings =
Please take a look at the following code... $(#page_info_select_box).live(keypress,function(){ var sele = domain; var
Before I ask my question, please take a look at this example function: DateTime.TryParse(01/01/2000,
Please take a look at this code: $(document).ready(function() { var urls = ['http://en.wikipedia.org/w/api.php?action=query&titles=File:Einstein2.jpg&prop=imageinfo&iiprop=url&iiurlwidth=144&format=json&callback=?', 'http://en.wikipedia.org/w/api.php?action=query&titles=File:Da_Vinci_Vitruve_Luc_Viatour.jpg&prop=imageinfo&iiprop=url&iiurlwidth=144&format=json&callback=?',

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.