在K&放位翻转功能; R运动2-7 [英] Bit invert function in K&R exercise 2-7

查看:136
本文介绍了在K&放位翻转功能; R运动2-7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

锻炼2-7的 C程序设计语言的:

写一个函数反转(X,P,N)返回 X N 即开始在位置 p 反转(即1变为0,反之亦然)位,保留其他不变。

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

我的理解是这样的问题:我有182它是 101(101)10 二进制,括号中的部分有不改变其他部分反转。返回值应 10101010 然后,即十进制数170。

I understood the question like this: I have 182 which is 101(101)10 in binary, the part in parentheses has to be inverted without changing the rest. The return value should be 10101010 then, which is 170 in decimal.

下面是我的尝试:

#include <stdio.h>

unsigned int getbits(unsigned int bitfield, int pos, int num);
unsigned int invert(unsigned int bitfield, int pos, int num);

int main(void)
{
    printf("%d\n", invert(182, 4, 3));
    return 0;
}

/* getbits: get num bits from position pos */
unsigned int getbits(unsigned int bitfield, int pos, int num)
{
    return (bitfield >> (pos+1-n)) & ~(~0 << num);
}

/* invert: flip pos-num bits in bitfield */
unsigned int invert(unsigned int bitfield, int pos, int num)
{
    unsigned int mask;
    unsigned int bits = getbits(bitfield,pos,num);

    mask = (bits << (num-1)) | ((~bits << (pos+1)) >> num);

    return bitfield ^ mask;
}

这似乎是正确的(对我来说),但反转(182,4,3)输出 536870730 getbits()工作正常(这是直接从书)。我写下的前pression我分配到会发生什么:

It seems correct (to me), but invert(182, 4, 3) outputs 536870730. getbits() works fine (it's straight from the book). I wrote down what happens in the expression I've assigned to y:

(00000101 << 2) | ((~00000101 << 5) >> 3)    -- 000000101 is the part being flipped: 101(101)10
       00010100 | ((11111010 << 5) >> 3)
       00010100 | (01000000 >> 3)
       00010100 | 00001000
= 00011100

  10110110 (182)
^ 00011100
----------
= 10101010 (170)

应该是正确的,但是事实并非如此。我发现这是哪里出了问题:((〜XPN&LT;&LT;(P + 1))&GT;&GT; N)。我不知道怎么样。

另外,我不知道这code如何将军。我的首要任务是取得这种情况下工作。帮助在这个问题上是值得欢迎的了。

Also, I've no idea how general this code is. My first priority is to just get this case working. Help in this issue is welcome too.

推荐答案

((1U&LT;&LT; N)-1)是一个位掩码用<$ C $ ç> N 在RHS1比特。 &LT;&LT; P 那些转移此块 P 左侧位置。 (你应该转向(P-N)而不是p如果你想从左边数)。

((1u<<n)-1) is a bit mask with n '1' bits at the RHS. <<p shifts this block of ones p positions to the left. (you should shift with (p-n) instead of p if you want to count from the left).

return val  ^ (((1u<<n)-1) <<p) ;

有仍然是一个问题,当p是大于字长大(由比字长更未定义移),但应该呼叫者的人的职责; - )

There still is a problem when p is larger than the wordsize (shifting by more than the wordsize is undefined), but that should be the responsability of the caller ;-)

有关的例子 101(101)10 与P = 2和n = 3:

For the example 101(101)10 with p=2 and n=3:

1u<<n               := 1000
((1u<<n)-1)         := 0111
(((1u<<n)-1) <<p) := 011100
original val    := 10110110
val ^ mask      := 10101010

这篇关于在K&放位翻转功能; R运动2-7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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