473,412 Members | 2,087 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,412 software developers and data experts.

Mistake in solutions to K&R?

This post lists a possible error in the solution to Exercise 2-7, page
49 (K&R) on the webpage
http://users.powernet.co.uk/eton/kandr2/krx207.html
Question:

Write a function invert(x,p,n) that returns x with the n bits that
begin at position p inverted (i.e., 1 changed into 0 and vice versa),
leaving the others unchanged.

The return statement on the webpage is

return x ^ (~(~0U << n) << p);

which is (possibly) incorrect; I've made the following one which is
correct (please correct me if it isn't):

return x^((~(~0U<<(p+1))>>(p+1-n))<<(p+1-n));

Explanation:

Take a number 1 1000 1000, say, and p=7 and n=4 then the answer should
be 1 0111 1000

In the original code the following steps take place:

1) 0000 0000 0000 0000 0000 0000 0000 0000 //0U

2) 1111 1111 1111 1111 1111 1111 1111 1111 //~

3) 1111 1111 1111 1111 1111 1111 1111 0000 //<<n

4) 0000 0000 0000 0000 0000 0000 0000 1111 //~

5) 0000 0000 0000 0000 0000 0111 1000 0000 //<< p

6) 0000 0000 0000 0000 0000 0001 1000 1000 //x itself

7) 0000 0000 0000 0000 0000 0110 0000 1000 //x^ (value in
step 5)

which is the wrong answer.

In my code, the following steps take place:

1) 0000 0000 0000 0000 0000 0000 0000 0000 //0U

2) 1111 1111 1111 1111 1111 1111 1111 1111 //~

3) 1111 1111 1111 1111 1111 1111 0000 0000 //<<p+1

4) 0000 0000 0000 0000 0000 0000 1111 1111 //~

5) 0000 0000 0000 0000 0000 0000 0000 1111 //>>p+1-n

6) 0000 0000 0000 0000 0000 0000 1111 0000 //<<p+1-n

7) 0000 0000 0000 0000 0000 0001 1000 1000 //x itself

7) 0000 0000 0000 0000 0000 0001 0111 1000 //x^ (value in
step 6)

which is, hopefully, the correct answer.

Examples

#1. (Same as above)

Take the number

392.d=110001000.b

Then if p=7 and n=4, then the answer after reversal will be:

376.d=101111000.b

This is what the code I made gives as the answer.

The answer the code on the website gives is

1544.d=11000001000.b

#2.

Take the number

588.d=1001001100.b

Then if p=7 and n=7, then the answer after reversal will be:

690.d=1010110010.b

This is what the code I made gives as the answer.

The answer the code on the website gives is

15820.d=11110111001100.b
----------------------------------------------------------------------------------------------------------------------
P.S: in the main() function on the same page, which was written "in a
hurry while tired", it's likely that n and p have been accidentally
reversed (that's not a mistake, anyway).

================================================== =========
Do you make Adsense Websites?
If the answer is yes, checkout this website out:
http://www.uniquecontentpro.com/?a=1231

Jun 27 '06 #1
4 2567
poodles said:
This post lists a possible error in the solution to Exercise 2-7, page
49 (K&R) on the webpage
http://users.powernet.co.uk/eton/kandr2/krx207.html


I have no control over that site any more, so I can't make any corrections
that may be necessary.

http://www.clc-wiki.net/ has taken over the maintenance of the answers page.
The relevant page is
http://www.clc-wiki.net/wiki/K%26R2_...2%3AExercise_7

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 27 '06 #2
In article <11**********************@i40g2000cwc.googlegroups .com> "poodles" <ch*****@gmail.com> writes:
This post lists a possible error in the solution to Exercise 2-7, page
49 (K&R) on the webpage
http://users.powernet.co.uk/eton/kandr2/krx207.html
Question:

Write a function invert(x,p,n) that returns x with the n bits that
begin at position p inverted (i.e., 1 changed into 0 and vice versa),
leaving the others unchanged.

The return statement on the webpage is

return x ^ (~(~0U << n) << p);


Should be x ^ (~(~0U << n) << (p + 1 - n)).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 27 '06 #3
On Tue, 27 Jun 2006 10:22:43 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalid> wrote:
poodles said:
This post lists a possible error in the solution to Exercise 2-7, page
49 (K&R) on the webpage
http://users.powernet.co.uk/eton/kandr2/krx207.html


I have no control over that site any more, so I can't make any corrections
that may be necessary.


In that case, is there some way to get the site dropped? It seems
risky to have your name attached to something you no longer control.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 27 '06 #4
Mark McIntyre said:
On Tue, 27 Jun 2006 10:22:43 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalid> wrote:
poodles said:
This post lists a possible error in the solution to Exercise 2-7, page
49 (K&R) on the webpage
http://users.powernet.co.uk/eton/kandr2/krx207.html


I have no control over that site any more, so I can't make any corrections
that may be necessary.


In that case, is there some way to get the site dropped? It seems
risky to have your name attached to something you no longer control.


It doesn't seem to have done me any harm, except for situations such as the
current one. Asking the provider to drop the site is likely to break quite
a few live links on other sites, and I'm loathe to initiate that process
(although I know it's bound to happen anyway, eventually).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 27 '06 #5

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

Similar topics

0
by: Will | last post by:
Hi all, Please excuse the longish post. I'm new to Python (and programming), but love what I see and believe it to be an important language. I have (at least) one retail client (with single...
4
by: poodles | last post by:
This post lists a possible error in the solution to Exercise 2-7, page 49 (K&R) on the webpage http://users.powernet.co.uk/eton/kandr2/krx207.html Question: Write a function invert(x,p,n)...
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
7
by: Ioannis Vranos | last post by:
In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html> there are some ambiguous errata, for which I propose solutions. Any comments are welcome. Ambiguous...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.