getbits k&r2

A

aegis

page 45 "consider the function getbits(x,p,n) that returns
the (right adjusted) n-bit field of x that begins at
position p. We assume that bit position 0 is at the right
end and that n and p are sensible positive values.
For example, getbits(x,4,3) returns the three bits in
positions 4, 3 and 2, right-adjusted."

1. What does he mean by right adjusting the value
and in what way is it right adjusted?

2. Why does he insist on right adjusting it?
Why should it be right adjusted when the same
function can be implemented without right adjusting
anything at all.

Mainly question 1 arises from not understanding
why he insists on right adjusting the value of x.

basically this function is suppose to return N bits
from position P

take a look at the number 10(ten) in binary, 1010_2

if we return 4 bits from position P then that is including
position P. As the starting point FROM position P connotes
belonging to position P. so it is logical to include that
as it is our logical starting point.

But look what happens

getbits(10(ten), 4, 4);

where getbits is,

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

4+1 = 5 and 5-4 = 1
10 >> 1, then in binary would look like:
(101_2)

and after we create our mask(1111_2)
and AND with 101_2, we get 101_2
which is 5. Which is not what we should get.
We should get 1010_2

And no, it is no coincedence that many people
find the wording of sentences in k&r to be frustrating
to read. Mainly due to it being written in a vague
manner. So apologies if my post comes off as being
written by someone angry.
 
I

infobahn

aegis said:
page 45 "consider the function getbits(x,p,n) that returns
the (right adjusted) n-bit field of x that begins at
position p. We assume that bit position 0 is at the right
end and that n and p are sensible positive values.
For example, getbits(x,4,3) returns the three bits in
positions 4, 3 and 2, right-adjusted."

1. What does he mean by right adjusting the value
and in what way is it right adjusted?

Consider getbits(42, 4, 3)

76543210
42, in binary: 00101010

Three bits starting at pos 4: 010

Right-adjusting merely means that these three bits will be the
rightmost bits of the output. So the required output in this
case would be 2 (i.e. lots of 0s with 010 at the end).
2. Why does he insist on right adjusting it?

It's already quite easy. Not right-adjusting would perhaps make it
too trivial.
Why should it be right adjusted when the same
function can be implemented without right adjusting
anything at all.

This makes no sense.
And no, it is no coincedence that many people
find the wording of sentences in k&r to be frustrating
to read. Mainly due to it being written in a vague
manner. So apologies if my post comes off as being
written by someone angry.

When you can do better, let us know. I found K&R much easier to
read than your article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top