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.
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.