K&安培; R第:需要帮助理解和QUOT; getbits()"方法2章 [英] K & R Question: Need help understanding "getbits()" method in Chapter 2

查看:214
本文介绍了K&安培; R第:需要帮助理解和QUOT; getbits()"方法2章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我前面提到的,我经历K&安培; R,和整体在做没事吧。然而,在第2章(第2.9节)对位运算符节,我无法理解怎么样的方法之一工作 - 作为一个结果,我在与相关的练习麻烦。

As I've mentioned before, I'm going through K&R, and overall am doing all right with it. However, in chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding how one of the sample methods works -- and as a result, I'm having trouble with the associated exercises.

(这不是对位移我以前问题的欺骗,顺便说一句,这是新和改善!)

(This isn't a dupe of my prior question on bit-shifting, btw. This is new-and-improved!)

所以这里提供的方法:

unsigned int getbits(unsigned int x, int p, int n) {
    return (x >> (p + 1 - n)) & ~(~0 << n);
}

我们的想法是,对于给定数量的 X 的,它会返回的 N 的起始位置比特的 P 的,从右边计数(与最右边位位置为0)。考虑下面的main()方法:

The idea is that, for the given number x, it will return the n bits starting at position p, counting from the right (with the farthest right bit being position 0). Given the following main() method:

int main(void) {
    int x = 0xF994, p = 4, n = 3;
    int z = getbits(x, p, n);
    printf("getbits(%u (%x), %d, %d) = %u (%X)\n", x, x, p, n, z, z);

    return 0;
}

输出是:

getbits(63892(f994),4,3)= 5(5)

getbits(63892 (f994), 4, 3) = 5 (5)

我得到这个部分,但我有难同大画面,因为位的大多是(没有双关语意),我不明白。

I get portions of this, but am having trouble with the "big picture," mostly because of the bits (no pun intended) that I don't understand.

(请注意,我的的寻找与相关的练习帮助 - 我想如果我得到这个,我能够得到这些信息)

(Note that I'm not looking for help with the associated exercises -- I think if I "got" this, I'd be able to get those.)

我专门遇到问题的部分是补充片:〜(〜0℃;&LT; N)。我觉得我得到的第一部分,处理的 X 的;它的这一部分(然后蒙版)我正在挣扎 - 如何这一切都在一起,其实检索这些位。 (我已经验证它是干什么的,都与code和检查使用的calc.exe我的结果 - !感谢上帝,它有一个二进制视图)

The part I'm specifically having issues with is the complements piece: ~(~0 << n). I think I get the first part, dealing with x; it's this part (and then the mask) that I'm struggling with -- and how it all comes together to actually retrieve those bits. (Which I've verified it is doing, both with code and checking my results using calc.exe -- thank God it has a binary view!)

任何帮助吗?

推荐答案

让我们用16位为我们的榜样。在这种情况下,〜0等于

Let's use 16 bits for our example. In that case, ~0 is equal to

1111111111111111

当我们左移(在你的案件3)本 N 位,我们得到:

When we left-shift this n bits (3 in your case), we get:

1111111111111000

因为 1 ■在左侧被丢弃和 0 ,则在适当的加入。然后重新补充它给:

because the 1s at the left are discarded and 0s are fed in at the right. Then re-complementing it gives:

0000000000000111

所以它只是一个聪明的方式来获得 N 的数量最少显著的部分1位。

so it's just a clever way to get n 1-bits in the least significant part of the number.

在X位你描述已经转移给定的数字(f994)右远足以使最不显著3位是你想要的。在这个例子中,你请求位被包围'。字符。

The "x bit" you describe has shifted the given number (f994) right far enough so that the least significant 3 bits are the ones you want. In this example, the bits you're requesting are surrounded by '.' characters.

ff94             11111111100.101.00  # original number
>> p+1-n     [2] 0011111111100.101.  # shift desired bits to right
& ~(~0 << n) [7] 0000000000000.101.  # clear all the other (left) bits

和你有你的位。ただ!

这篇关于K&安培; R第:需要帮助理解和QUOT; getbits()&QUOT;方法2章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆