0

In the K&R book in chapter 2.9, I am stuck on understanding this sample function getbits()

getbits(x,p,n)

Returns the (right-adjusted) n-bit field of x that begins at position p. Here's the function body

/* getbits: get n bits from from position p */
unsigned getbits(unsigned x, int p, int n)
{
   return (x >> (p+1-n)) & ~(~0 << n);
}

I am actually not facing problems with the bitwise operators but I can't actually get the question. I am stuck on understanding the question mainly not solving it. Ultimately "What do we need to find in this function".

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
  • 1
    You may find this tool helpful: http://bitwisecmd.com/ – WhiZTiM May 11 '16 at 13:19
  • 3
    in ANSI C this code is undefined behaviour, so don't worry about it too much. – M.M May 11 '16 at 13:22
  • @Bathsheba please reopen the question , i have asked this question because the answers of the other question didn't satisfy me – Some_Random_Programmer May 11 '16 at 13:29
  • Hum. That's not a good reason for re-opening a question closed as a duplicate. If you want more attention to be given to the original question then offer a bounty on it. Of course that requires reputation but you can acquire that by asking good questions and submitting good answers, and suggesting good edits. – Bathsheba May 11 '16 at 13:36
  • 2
    Run your code in a debugger (or add print statements) so that you can see what's going on. – KevinDTimm May 11 '16 at 13:55
  • 1
    There is no point in re-opening it until you have made some effort. Compile, build, test, step through with debugger, as suggested by @KevinDTimm. If you don't make some effort, your question may be accidentally interpreted as a no-effort homework dump where you just want someone else to write it up and give a copypasta example. I'm sure you don't want that. – Martin James May 11 '16 at 13:59
  • The [accepted answer in the linked duplicate](http://stackoverflow.com/a/197652/253056) is pretty clear - I'm not sure that there is much room for improvement. – Paul R May 11 '16 at 14:08
  • I've always strongly spoken out against K&R but upon seeing this _pure crap_... I thought this was some failed homework attempt by our average confused beginner. But no, this is from the book itself. Is that 2 or 3 severe cases of undefined behavior on a single line? Worst code posted on SO today. Seriously, programmers around the world need to unite and organize book burnings of K&R! – Lundin May 11 '16 at 14:17
  • @Lundin - in no case should K&R be burned, a huge number of very qualified, successful C programmers came from that book. What should happen is that questions pertaining to K&R should be 'relegated' to a special section/maybe even a new tag in SO as K&R does not reflect properly upon the state of C programming today. – KevinDTimm May 11 '16 at 15:51
  • @KevinDTimm The only reason for that is because there were no other books in the late 70s. The upgrade to 2nd edition was just sheer sloppy, either they didn't understand the ISO standard or they couldn't be arsed to read it. The book went obsolete in 1989. Why people still read it in 2016, I have absolutely no idea. Doing so is harmful and will make you a worse programmer. – Lundin May 11 '16 at 19:41
  • @Lundin - and I don't disagree that it's not relevant today. Just like it's possible to build (and operate) a foot driven lathe it's the same for K&R. I really think though that we should tag these as K&R (it exists) as wasting time on these questions really doesn't help anybody. `kernighan-and-ritchie` tag added! – KevinDTimm May 12 '16 at 13:21

2 Answers2

1

It just shifts the bits right by p+1-n bits (to make the required field right-justified), then masks out all but the bottom n bits. Here is an expanded version, which may be easier to understand:

unsigned getbits(unsigned x, int p, int n)
{
   unsigned x_right = (x >> (p+1-n)); // shift right to make required bits right-justified
   unsigned mask = ~(~0 << n);        // create n bit mask
   return x_right & mask;             // return required bits
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
0

Drawing a binary representation usually helps for such cases.

For example: taking 3 bits from position 5:

 X: 101010101
     --^
       p

It shifts:

 X: 1010
       ^
       p

And then masks:

 X:  010
     --^
       p
Vasfed
  • 18,013
  • 10
  • 47
  • 53