Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#向左旋转位溢出问题_C#_Bit - Fatal编程技术网

C#向左旋转位溢出问题

C#向左旋转位溢出问题,c#,bit,C#,Bit,几天来,我一直在努力让它发挥作用,我已经阅读了上千本指南和人们的问题,但我仍然找不到一个正确的方法 我想做的是将位向左旋转,下面是一个例子 原始编号=10000001=129 我需要的=00000011=3 我必须将位向左旋转一定的次数(这取决于用户类型),下面是我所做的: byte b = (byte)129; byte result = (byte)((byte)b << 1); Console.WriteLine(result); Console.Write("Pr

几天来,我一直在努力让它发挥作用,我已经阅读了上千本指南和人们的问题,但我仍然找不到一个正确的方法

我想做的是将位向左旋转,下面是一个例子

原始编号=10000001=129
我需要的=00000011=3

我必须将位向左旋转一定的次数(这取决于用户类型),下面是我所做的:

byte b = (byte)129;
byte result = (byte)((byte)b << 1);
Console.WriteLine(result);

Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
字节b=(字节)129; 字节结果=(字节)((字节)b
precodeb=(b>7)|((b&0x7f)很明显,您得到了一个溢出异常,因为您正在检查的上下文中操作

您可以通过将代码放在未检查的上下文中,或者只通过确保不对大于255的值执行回溯到
byte
的操作来解决此问题。例如:

int shifted = b << rotateLeftBits;
int highBits = shifted & 0xff;
int lowBits = shifted >> 8; // Previously high bits, rotated
byte result = (byte) (highBits | lowBits);
int-shift=b>8;//以前的高位,已旋转
字节结果=(字节)(高位|低位);

这将适用于最大为8的旋转大小。对于更大的大小,只需使用
rotateLeftBits%8
(如果您有时想向右旋转,请将其标准化为非负数)。

谢谢您的回答

在我发表这篇文章后不久,我想到了一个解决这个问题的方法,让我向你展示一下(在你提问之前,它是有效的!):

字节b=(字节)129; b=(字节)((字节)b&127);
字节结果=(字节)((字节)b请尝试此函数-双向旋转(8位值的左右旋转)[我没有测试该函数!]

// just for 8Bit values (byte)
byte rot(byte value, int rotation)
    {
        rotation %= 8;

        int result;
        if(rotation < 0)
        {
            result = value << (8 + rotation);
        }
        else
        {
            result = value << rotation;
        }

        byte[] resultBytes = BitConverter.GetBytes(result);
        result = resultBytes[0] | resultBytes[1];

        return (byte)result;
    }

short rot(short value, int rotation) { ... }
int rot(int value, int rotation) { ... }
//仅用于8位值(字节)
字节旋转(字节值,整数旋转)
{
旋转%=8;
int结果;
如果(旋转<0)
{

结果=值您是否尝试过使用未检查的关键字?您是否只使用字节?如果是这样,您可以通过将其作为int进行移位,然后在完成后使用掩码对其进行截断来欺骗位,可能会使用不同的掩码和移位来获得放在低端的位。当我这样做时,我不会得到
OverflowException
。您将显然,我需要一个演员阵容(linqpad告诉我)。我喜欢在不同的轮班数量下工作我认为这不太正确。你的第四行将低位设置为1,而不管高位是从什么开始的。例如,位模式00000001变为00000011。正如我之前所说,这只是更大代码的一部分,在完整的代码中,有一个数字和128的运算来知道第一位是1还是0,并且依赖于结果是,它将输入IF或the ELSE,在IF上我会做我写下的事情,在ELSE上我会做正常的事情
byte b = (byte)129; 
b = (byte)((byte)b & 127); 
byte result = (byte)((byte)b << 1); 
result = (byte)((byte)result | 1); 
Console.WriteLine(result); 
// just for 8Bit values (byte)
byte rot(byte value, int rotation)
    {
        rotation %= 8;

        int result;
        if(rotation < 0)
        {
            result = value << (8 + rotation);
        }
        else
        {
            result = value << rotation;
        }

        byte[] resultBytes = BitConverter.GetBytes(result);
        result = resultBytes[0] | resultBytes[1];

        return (byte)result;
    }

short rot(short value, int rotation) { ... }
int rot(int value, int rotation) { ... }