按位旋转 [英] bitwise rotation

查看:79
本文介绍了按位旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,


我在C#中模拟汇编语言位轮换并且工作原理

奇妙


---------

....

public uint价值;

....

public uint RotateRight(字节数){

Value =((Value>> count)|(Value<<(32-count)));

返回值;

}

---------


只要是值是32位还是64位(由于

移位运算符的限制)。我需要它来处理8位或16位值。我b $ b假设我必须知道价值的预期大小并且确定转移并移动该位和0或7,或0或者15位的位置并清除

如果有的额外位。这似乎是很多工作。


有没有办法模拟8位或16位旋转32位或64位

价值?

谢谢,

Shawn

Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
....
public uint Value;
....
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
presume I''d have to have knowledge of the intended size of the value and do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn

推荐答案




public uint RotateRight16(uint值,字节数)

{

return((值>>计数) )|(值<<(16-Count)))& 0xffff;

}

public uint RotateRight8(uint值,字节数)

{

return((值>>计数)|(值<<(8-Count)))& 0xff;

}


public uint RotateLeft16(uint值,字节数)

{

return((Value<< Count)| (值>>(16-Count)))& 0xffff;

}

public uint RotateLeft8(uint值,字节数)

{

return((Value<< Count)|(Value>>(8-Count)))& 0xff;

}


-

Robert Jeppesen

robert.jeppesen%at%durius-dot-se

" Shawn B." <乐**** @ html.com>在留言新闻中写道:O9 *************** @ TK2MSFTNGP11.phx.gbl ...
How about:

public uint RotateRight16(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (16-Count)))&0xffff;
}
public uint RotateRight8(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (8-Count)))&0xff;
}

public uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (16-Count)))&0xffff;
}
public uint RotateLeft8(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (8-Count)))&0xff;
}

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:O9***************@TK2MSFTNGP11.phx.gbl...
问候,

我是在C#中模拟汇编语言位旋转并且它的工作原理非常好

---------
...
公共uint值;
...
public uint RotateRight(字节数){
Value =((Value>> count)|(Value<<(32-count)));
返回值;
}
---------

只要值为32位或64位(由于受限制
班次操作员)。我需要它来处理8位或16位值。我认为我必须知道该值的预期大小,并且确定移位并移动位和0或7,或0或15位位置并清除
如果有的额外位。这看起来很多工作。

有没有办法模拟具有32位或64位
值的8位或16位旋转?

谢谢,
Shawn
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
presume I''d have to have knowledge of the intended size of the value and do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn



糟糕,忘了确保价值在限制范围内:


public static uint RotateRight16(uint Value,byte Count)

{

return(((Value& 0xffff)>> Count)|(值<<(16-Count)))& 0xffff;

}

public static uint RotateLeft16(uint Value,byte Count)

{

return((Value<< Count)|((Value& 0xffff)>>(16-Count)))& 0xffff;

}


等8位。


-

Robert Jeppesen

robert.jeppesen%at%durius-dot-se

" Robert Jeppesen" < robert.jeppesen(#)durius.se>在留言新闻中写道:eE ************** @ TK2MSFTNGP11.phx.gbl ...
Oops, forgot to make sure that value is within limits:

public static uint RotateRight16(uint Value, byte Count)
{
return (((Value&0xffff) >> Count) | (Value << (16-Count)))&0xffff;
}
public static uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | ((Value&0xffff) >> (16-Count)))&0xffff;
}

etc for 8-bit.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message news:eE**************@TK2MSFTNGP11.phx.gbl...
怎么样:

public uint RotateRight16(uint Value,byte Count)
返回((Value>> Count)|(Value<<(16-Count)))& 0xffff;
}
public uint RotateRight8(uint Value,byte Count)
{
return((Value>> Count)|(Value<<(8- Count)))& 0xff;
}
公共uint RotateLeft16(uint值,字节数)
返回((值<< Count) |(值>>(16-Count)))& 0xffff;
}
public uint RotateLeft8(uint Value,byte Count)
{
return((Value) << Count)|(Value>>(8-Count)))& 0xff;
}

-
Robert Jeppesen
罗伯特.jeppesen%at%durius-dot-se

" Shawn B." <乐**** @ html.com>在留言新闻中写道:O9 *************** @ TK2MSFTNGP11.phx.gbl ...
How about:

public uint RotateRight16(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (16-Count)))&0xffff;
}
public uint RotateRight8(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (8-Count)))&0xff;
}

public uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (16-Count)))&0xffff;
}
public uint RotateLeft8(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (8-Count)))&0xff;
}

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:O9***************@TK2MSFTNGP11.phx.gbl...
问候,

我是在C#中模拟汇编语言位旋转并且它的工作原理非常好

---------
...
公共uint值;
...
public uint RotateRight(字节数){
Value =((Value>> count)|(Value<<(32-count)));
返回值;
}
---------

只要值为32位或64位(由于受限制
班次操作员)。我需要它来处理8位或16位值。我认为我必须知道该值的预期大小,并且确定移位并移动位和0或7,或0或15位位置并清除
如果有的额外位。这看起来很多工作。

有没有办法模拟具有32位或64位
值的8位或16位旋转?

谢谢,
Shawn
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
presume I''d have to have knowledge of the intended size of the value and do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn




很难相信它是如此简单,我试过那...除非你在哪里
16-count是,我把它留在64位的8位掩码,这就解释了为什么它b / b
错了。


非常感谢,

Shawn

" Robert Jeppesen" < robert.jeppesen(#)durius.se>在消息中写道

新闻:eE ************** @ TK2MSFTNGP11.phx.gbl ...
Hard to believe it was so simple, I tried that... except where you''re
16-count is, I left it at 64 bits with the 8-bit mask, that explains why it
was wrong.

Thanks very much,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:eE**************@TK2MSFTNGP11.phx.gbl...
怎么样

public uint RotateRight16(uint Value,byte Count)
{
return((Value>> Count)|(Value<<( 16-Count)))& 0xffff;
}
public uint RotateRight8(uint Value,byte Count)
{
return((Value>> Count)|(值<<(8-Count)))& 0xff;
}
公共uint RotateLeft16(uint值,字节数)
{
返回( (值<< Count)|(值>>(16-Count)))& 0xffff;
}
public uint RotateLeft8(uint Value,byte Count)
{
return((值<< Count)|(Value>>(8-Count)))& 0xff;
}

-
Robert Jeppesen
robert.jeppesen%at%durius-dot-se

Shawn B. <乐**** @ html.com>在消息中写道
How about:

public uint RotateRight16(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (16-Count)))&0xffff;
}
public uint RotateRight8(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (8-Count)))&0xff;
}

public uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (16-Count)))&0xffff;
}
public uint RotateLeft8(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (8-Count)))&0xff;
}

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message



新闻:O9 *************** @ TK2MSFTNGP11.phx.gbl ...


news:O9***************@TK2MSFTNGP11.phx.gbl...

问候,

我在C#中模拟汇编语言位旋转,它的工作原理很好

------ ---
...
公共uint值;
...
公共uint RotateRight(字节数){
值=((值>> count)|(值<<(32-count)));
返回值;
}
---------

只要该值为32位或64位(由于
移位运算符的限制)。我需要它来处理8位或16位值,因为
很好。我认为我必须知道值的预期大小,并且
执行移位并移动位和0或7,或0或15位位置和
清除额外位如果有的话。这看起来很多工作。

有没有办法模拟具有32位或64位
值的8位或16位旋转?

谢谢,
Shawn
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I presume I''d have to have knowledge of the intended size of the value and do the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn




这篇关于按位旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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