本文介绍了K&安培; R C练习帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历的K&放大器; R C语言程序设计的书,我卡在练习2-6其内容为:

I've been going through the K&R C Programming Language book and I'm stuck on Exercise 2-6 which reads:

写函数setbits(X,P,N,Y)与该开始在位置p设定为y的最右边的n位的n位返回x,而使其他位不变。

我无法理解确切的事情,他们正在寻找我做的。我看着一个可能的答案 rel=\"nofollow\">,但我还是真的不理解。我认为这是的投掷我送行的措辞。任何人都可以或许解释他们在寻找什么我以不同的方式吗?我希望不同的措辞会帮助我了解什么,我需要做的code明智的。

I'm having trouble understanding the exact thing they're looking for me to do. I looked at a possible answer here, but I still don't really understand. I think it's the wording that's throwing me off. Can anyone maybe explain what they're looking for me to do in a different way? I'm hoping that different wording will help me understand what I need to do code wise.

推荐答案

在简释阿维的回答是:

int i = setbits(0xAB = b10101011, 5, 3, 0xAA = b10101010);
i equals 0x93 = b10010011

说你的我=是0xAB。在二进制,这就是:10101011

Say your i = 0xAB. In binary, this is: 10101011

让我们每一个比特位置的。数

Let's number each of the bit positions.

Position #: 7   6   5   4   3   2   1   0
Bit:        1   0   1   0   1   0   1   1

最右边的位(最低的显著)是位置0。最左边的(最显著)是位置7

The right-most bit (the least-significant) is position "0". The left-most (most-significant) is position "7".

因此​​,接下来两个值,p型和n,都在说:你要修改n位起始位P。所以,如果P = 5和n = 3,你想在位5号开始,并在总你修改3位。这意味着在本实施例中的位5,4,3101。

So the next two values, p and n, are saying "You want to modify n bits starting at bit p." So if p=5 and n=3, you want to start at bit number 5, and in total you're modifying 3 bits. Which means bits 5, 4, 3. "101" in this example.

Position #: 7   6   5   4   3   2   1   0
Bit:        1   0   1   0   1   0   1   1
                   |         |
                    ---------
               (Modifying these three bits)

我们如何修改呢?我们正在取代他们。与另一组的3比特。从y中的三个最低-显著位

How are we modifying them? We are replacing them. With another set of 3 bits. The three least-significant bits from y.

因此​​,这里的Y:

Position #: 7   6   5   4   3   2   1   0
Bit:        1   0   1   0   1   0   1   0

和最右边的比特将是位2,1,0或值010。当然,如果n = 6的价值,那么你想从我与101010,取代那些六位 - 最右边的6位

And the right-most bits would be bits 2, 1, 0. or the value "010". Of course, if the value of n=6, then you'd want to replace those six bits from i with "101010" - the rightmost 6 bits.

所以,你的任务是从我坐指定位 - 在这种情况下,101 - 与y中的指定位取代他们 - 010

So your task is to take the specified bits from i - in this case, "101" - and replace them with the specified bits in y - "010".

如果你这样做,那么你的返回值为

If you do this, then your return value is

1 0 1 0 1 0 1 0

1 0 1 0 1 0 1 0

这篇关于K&安培; R C练习帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!