K&RC 锻炼帮助

发布于 2024-08-04 09:30:10 字数 353 浏览 2 评论 0原文

我一直在阅读《K&RC 编程语言》这本书,但我一直停留在练习 2-6 上,其中内容如下:

编写一个函数 setbits(x,p,n,y),返回 x,并将从位置 p 开始的 n 位设置为 y 最右边的 n 位,其他位保持不变。

我很难理解他们到底要我做什么。我在此处查看了可能的答案,但我仍然不知道理解。我认为正是这些措辞让我感到困惑。谁能用不同的方式解释一下他们要我做什么?我希望不同的措辞能够帮助我理解我需要明智地执行代码。

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

Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

倾城月光淡如水﹏ 2024-08-11 09:30:10

解释 Avi 的答案:

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

假设你的 i = 0xAB。在二进制中,这是: 10101011

让我们对每个位位置进行编号。

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

最右边的位(最低有效位)是位置“0”。最左边(最重要)是位置“7”。

因此,接下来的两个值 p 和 n 表示“您想要修改从 p 位开始的 n 位”。因此,如果 p=5 且 n=3,您希望从第 5 位开始,总共要修改 3 位。这意味着位 5、4、3。本例中为“101”。

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

我们如何修改它们?我们正在替换它们。与另一组 3 位。 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 位)替换 i 中的这 6 位。

因此,您的任务是从 i 中获取指定位(在本例中为“101”),并将它们替换为 y 中的指定位(“010”)。

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

1 0 1 0 1 0 1 0

Expounding on Avi's answer:

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

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

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

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)

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

So here's y:

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

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.

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

没有伤那来痛 2024-08-11 09:30:10

例如:

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

我们取出 x (101) 中从位置 5 开始的 3 位,并将它们替换为 y (010) 中最右边的 3 位。

For example:

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

We take the 3 bits beginning at position 5 in x (101), and replace them with the rightmost three bits from y (010).

水波映月 2024-08-11 09:30:10

这个“可能的答案”只是没有注释的代码。难怪它对你没有帮助。

该问题(可能还有回答者)假设您熟悉位字段。这种事情在控制硬件寄存器的嵌入式编程中很常见。

假设有一个寄存器可以设置音频音量级别等。同时,它可能会让您选择扬声器或麦克风之类的东西。这些位可能如下所示:

ssAAAmxx - 每个字母代表该数字中的一个位字段。要更改音量,您必须更改“AAA”的值。现在,假设您的程序中有一些可以让您调节音量的东西。这是一个简单的控件,它总是返回 0 到 7 之间的数字。其格式如下所示:

xxxxxAAA - 那么您的工作是从中获取 AAA 位(称为“y”),并设置它们到上面的数字(称之为“x”),而不改变不是 A 的位。因此,问题将是:“取 y 最右边的 3 位,并将它们设置为 x,从第 5 位开始(记住,它们从零开始计数)。然后,我们示例中的 3 和 5 就变成了 n 和 p原来的问题。

That "possible answer" is just code without comments. No wonder it didn't help you.

The question (and probably the answerers) assume that you are familiar with bit fields. This sort of thing is very common in embedded programming where you control hardware registers.

Say there's a register that sets the audio volume level, among other things. It might, at the same time, let you select speakers or microphones and things like that. The bits might look like this:

ssAAAmxx -- Each letter represents a bitfield within that number. To change the volume, you have to alter the value of "AAA". Now, lets say you have something in your program that lets you adjust the volume. It's a simple control, and it always returns a number between 0 and 7. The format for that looks like this:

xxxxxAAA -- You job then, is to take the AAA bits from this (call it "y"), and set them into that number above (call it "x"), without altering the bits that aren't the A's. Thus, the problem would read, "Take the rightmost 3 bits of y, and set them into x, starting with bit 5 (remember, they count bits from zero). Then, 3 and 5 in our example become n and p in the original problem.

时光磨忆 2024-08-11 09:30:10

该操作是“位域插入”,

其想法是 y 通常会少于 n 位,但如果不是,则仅使用 n 。在英语中,任务是将 y 插入到 x 中,从 p 开始,使用字段宽度 n

The operation is "bitfield insert"

The idea is that y would usually be fewer than n bits, but in case it's not, only use n. In english, the task is to insert y into x beginning at p, using a field width of n.

百变从容 2024-08-11 09:30:10

将 x 的 n 位(从 p 位置开始)替换为 y 最右边的 n 位。

也许你应该利用第 2.9 章中的 getbits() 例程

Replace n bits of x, starting at p position, with the rightmost n bits of y.

And probably you should take advantage of the getbits() routine in chapter 2.9

长亭外,古道边 2024-08-11 09:30:10

这个怎么样?

unsigned int
setbits(unsigned int x, int p, int n, unsigned int y)
{
    char buffer[65];

    unsigned x1 = x >> (p + 1);
    x1 <<= (p + 1);

    /*
     * x1 now contains all the bits before position 'p'.
     */

    unsigned x2 = y & ~(~0 << n);
    x2 <<= (p + 1) - n;

    /*
     * x2 now contains the rightmost 'n' bits of 'y', left shifted (p + 1) - n bits.
     */
    unsigned x3 = x & ~(~0 << ((p + 1) - n));

    /*
     * x3 now contains the rightmost (p + 1) - n bits.
     */

    return x1 | x2 | x3;
}

How about this?

unsigned int
setbits(unsigned int x, int p, int n, unsigned int y)
{
    char buffer[65];

    unsigned x1 = x >> (p + 1);
    x1 <<= (p + 1);

    /*
     * x1 now contains all the bits before position 'p'.
     */

    unsigned x2 = y & ~(~0 << n);
    x2 <<= (p + 1) - n;

    /*
     * x2 now contains the rightmost 'n' bits of 'y', left shifted (p + 1) - n bits.
     */
    unsigned x3 = x & ~(~0 << ((p + 1) - n));

    /*
     * x3 now contains the rightmost (p + 1) - n bits.
     */

    return x1 | x2 | x3;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文