实施只用&QUOT逻辑右移;〜&安培; ^ | + LT;< >> = QUOT;运营商和20个操作 [英] Implementing logical right shift using only "~ & ^ | + << >> =" operators and 20 operations

查看:89
本文介绍了实施只用&QUOT逻辑右移;〜&安培; ^ | + LT;< >> = QUOT;运营商和20个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个使用只有约的位操作,​​与功放的分配,我要code函数在C; ,^,| ,+,所述;&下; ,>>,和=。我只使用20次。而且我不允许使​​用控制结构,例如,如果其他,对,同时,交换机或其它任何excutes code的条件块。另外的类型转换也退出并没有在函数头(这是给我)宣布的字节数,被限制在1个或者8位值;所以我通过FF十六进制有0。

So I have an assignment that I have to code a function in c that uses only the bitwise operations of ~ , & , ^ , | , + , << , >> , and =. I have to use only 20 operations. And I am not allowed to use control structures such as, if-else , for, while, switch, or anything else that excutes code in conditional blocks. Also type casting is also out and the bytes that are not declared in the function header (which is given to me), are limited to 1 byte or 8 bit values; so I have hex 0 through FF.

要code的作用我是一个合乎逻辑右移。因此,而不是位填充符号位应填写了0

The function I have to code is a logical right shift. So instead of the bits filling up with the sign bit they should fill up with 0's

这是我做了什么:

int logicalShift(int x, int n) {
    int op=0xFFFFFFFF;
    int tcn=(~n+1);
    int sizeshift=0x20 & tcn;
    op=(op<<sizeshift);
    return ((x>>n) + (op));
}

这是我希望得到(对于X = 0x80000000的,和n = 0×01)
我希望得到为0x40000000即十进制数1073741824。这是我得到的。
不过(对于X = 0x80000000的,和n =为0x0
我期望得到0x80000000的,但是我得到这为0x7FFFFFFF是我的答案减去了一下。我可以添加了一点,但它搅乱了第一个答案。所以,我是什么做错了,我有一个案例而不是其他。我也曾尝试。

This is what I expect to get ( for an x=0x80000000, and n=0x01) I expect to get 0x40000000 which is 1073741824 in decimal. This is what I get. However (for an x=0x80000000, and n=0x0 I expect to get 0x80000000, however I get 0x7fffffff which is my answer minus a bit. I could add a bit, but it messes up the first answer. So What am I doing wrong that I have one case but not the other. I have also tried.

int logicalShift(int x, int n) {
    int op=0xFFFFFFFF;
    int tcn=(~n+1);
    int sizeshift=0x20 & tcn;
    op=(op<<sizeshift);
    return ((x>>n) + (op  ^ ~n));
}

我想,如果我XOR设定清零为全1的符号位为0的情况下位的我最终的东西,这不是负(AKA)为0x7FFFFFFF,当它通过编译器转换成2的补去了。它结束了使情况变得更糟。请把我安置在我应该考虑和正确的方向,为什么?

I thought that if I XOR the set of bits zeroing out the sign bits with all 1's for the 0 case I would end up with something that was not negative (aka) 0x7fffffff when it went through the compilers conversion to 2's complement. It ended up making it worse. Please set me in the right direction what should I consider and why?

推荐答案

逻辑和算术移位的区别是位从左边移入。为了实现逻辑移位算术方面你可以做算术移位,然后清除新位。在伪code:

The difference between logical and arithmetic shift are the bits shifted in from the left. To implement logical shift in terms of arithmetic you can do the arithmetic shift and then clear the new bits. In pseudo-code:


  1. 生成的屏蔽,将清除最左边的 N 的位时,结果与操作。

  2. 右移ñ的位。

  3. 和面具。

  1. Generate a mask that will clear the leftmost n bits when ANDed with the result.
  2. Shift right by n bits.
  3. AND the mask.

使用和手段,你不必在意在移位位是什么,你只是想无条件地将它们设置为0。

Using AND means you don't have to care what bits are shifted in. You just want to unconditionally set them to 0.

接下来的问题是如何生成的掩模。我会离开,作为一个练习。

The question then is how to generate the mask. I'll leave that as an exercise for you.

这篇关于实施只用&QUOT逻辑右移;〜&安培; ^ | + LT;&LT; &GT;&GT; = QUOT;运营商和20个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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