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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:25:33+00:00 2026-05-10T17:25:33+00:00

In chapter 2, the section on bitwise operators (section 2.9), I’m having trouble understanding

  • 0

In chapter 2, the section on bitwise operators (section 2.9), I’m having trouble understanding how one of the sample methods works.

Here’s the method provided:

unsigned int getbits(unsigned int x, int p, int n) {     return (x >> (p + 1 - n)) & ~(~0 << n); } 

The idea is that, for the given number x, it will return the n bits starting at position p, counting from the right (with the farthest right bit being position 0). Given the following main() method:

int main(void) {     int x = 0xF994, p = 4, n = 3;     int z = getbits(x, p, n);     printf('getbits(%u (%x), %d, %d) = %u (%X)\n', x, x, p, n, z, z);      return 0; } 

The output is:

getbits(63892 (f994), 4, 3) = 5 (5)

I get portions of this, but am having trouble with the ‘big picture,’ mostly because of the bits (no pun intended) that I don’t understand.

The part I’m specifically having issues with is the complements piece: ~(~0 << n). I think I get the first part, dealing with x; it’s this part (and then the mask) that I’m struggling with — and how it all comes together to actually retrieve those bits. (Which I’ve verified it is doing, both with code and checking my results using calc.exe — thank God it has a binary view!)

Any help?

  • 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. 2026-05-10T17:25:34+00:00Added an answer on May 10, 2026 at 5:25 pm

    Let’s use 16 bits for our example. In that case, ~0 is equal to

    1111111111111111 

    When we left-shift this n bits (3 in your case), we get:

    1111111111111000 

    because the 1s at the left are discarded and 0s are fed in at the right. Then re-complementing it gives:

    0000000000000111 

    so it’s just a clever way to get n 1-bits in the least significant part of the number.

    The "x bit" you describe has shifted the given number (f994 = 1111 1001 1001 0100) right far enough so that the least significant 3 bits are the ones you want. In this example, the input bits you’re requesting are there, all other input bits are marked . since they’re not important to the final result:

    ff94             ...........101..  # original number >> p+1-n     [2] .............101  # shift desired bits to right & ~(~0 << n) [7] 0000000000000101  # clear all the other (left) bits 

    As you can see, you now have the relevant bits, in the rightmost bit positions.

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

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 79k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Try this. May 11, 2026 at 3:58 pm
  • added an answer You should use an inline tag like <span> May 11, 2026 at 3:58 pm
  • added an answer Does it have to be done within vim? Could you… May 11, 2026 at 3:58 pm

Related Questions

In chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding
Based on information in Chapter 7 of 3D Programming For Windows (Charles Petzold) ,
In chapter 8 of Godel, Escher, Bach by Douglas Hofstader, the reader is challenged
I'm trying to implement an HTML Parsing web service as described in Chapter 23
I've read chapter 7 in the 'Linux Device Drivers' (which can be found here)

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.