473,545 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(unsigne d 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 3536
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
7964
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
2145
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 give me strange behavior. Here is the code I've written: /****************************************************************************** * K&R2...
16
1665
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 somtimes confused about the difference between a two-dimensional array and an array of pointers..." then continues to explain int *b; to be...
3
1915
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 the one by Dennis Ritchie is good but can any one mention some other books as well Thanx.
2
2281
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 yet. i have created a solution myself by modifying the example programme of section 1.19. i tried to find the source-code of K&R2 using Google. i...
8
4745
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. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
19
2377
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 to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical
24
1774
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. I did two compiles shown below. $ gcc -std=c89 -Wall exercise_1-9.c -o exercise_1-9 exercise_1-9.c:5: warning: return type defaults to `int'
15
1797
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 match on the line. The match we have to find is a char array named pattern. We also print out the number of matches we have found. We will take the...
0
7425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.