473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getbits k&r2

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.

--
aegis

Nov 14 '05 #1
1 3530
aegis wrote:

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.
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David | last post by:
this phrase has been mentioned many times in this NG and it seems like everyone know what it's. is it a book? a standard? or a guide of some sort? where can i read more about K&R2? thanks!
12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
16
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are...
3
by: Sunny | last post by:
Hi, Well this might look like a dumb question to 99.99% of u but what exactly is K&R2 and where can I get it? Secondly alot of people say that you can learn by reading good books on C. I know...
2
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
24
by: Anthony Irwin | last post by:
Hi all, I have been going through the k&r2 book and all the examples are done with main() instead of int main(void) and k&r2 in the start of chapter 1 does not return 0 so I haven't yet either....
15
by: arnuld | last post by:
STATEMENT: Write the function strindex(s, t), which returns the position of the rightmost occurence of t in s, or -1 if there is none. PURPOSE: this program prints the index of the rightmost...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.