PC Review


Reply
Thread Tools Rate Thread

bitwise rotation in c#

 
 
Ramtin Kazemi
Guest
Posts: n/a
 
      21st Nov 2007
Hi
How can i perform bitwise rotation in C#?


 
Reply With Quote
 
 
 
 
DeveloperX
Guest
Posts: n/a
 
      21st Nov 2007
On 21 Nov, 14:17, "Ramtin Kazemi" <ramtin.kaz...@yahoo.com> wrote:
> Hi
> How can i perform bitwise rotation in C#?


int x = 1;
x<<=3;
Console.WriteLine(x.ToString()); //writes 8
 
Reply With Quote
 
=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=
Guest
Posts: n/a
 
      21st Nov 2007
DeveloperX wrote:
> On 21 Nov, 14:17, "Ramtin Kazemi" <ramtin.kaz...@yahoo.com> wrote:
>> Hi
>> How can i perform bitwise rotation in C#?

>
> int x = 1;
> x<<=3;
> Console.WriteLine(x.ToString()); //writes 8


Taking the question literally, that's not bitwise rotation, it's bitwise
shifting. The difference is what happens when you start shifting bits
off of one end, with shifting you lose them, with rotation they come
back in on the other end.

There is no built-in bitwise rotation operator in .NET that I'm aware
of, perhaps in 3.5 there could be something somewhere that I don't know
of, but in 2.0 I'm suspecting you have to roll your own.

--
Lasse Vågsæther Karlsen
private.php?do=newpm&u=
http://presentationmode.blogspot.com/
 
Reply With Quote
 
Ebbe Kristensen
Guest
Posts: n/a
 
      21st Nov 2007
DeveloperX wrote:
> On 21 Nov, 14:17, "Ramtin Kazemi" <ramtin.kaz...@yahoo.com> wrote:
>> Hi
>> How can i perform bitwise rotation in C#?

>
> int x = 1;
> x<<=3;
> Console.WriteLine(x.ToString()); //writes 8


Beware! Shift is not the same as rotate. When rotating, the MSBit is moved
to the LSBit position whereas in a shift opration, the LSBit is set to 0.

To illustrate using a 4-bit variable with the initial bitpattern 1100:

Value after shift: 1000

Value after rotate: 1001

A solution might look something like:

int Rotate( int x )
{
if( 0x80000000 & x )
return (x << 1) | 0x00000001;
else
return x << 1;
}

I haven't used C# for some time so this may be a bit "C++'ish" :-)

Ebbe


 
Reply With Quote
 
John Duval
Guest
Posts: n/a
 
      21st Nov 2007
On Nov 21, 9:17 am, "Ramtin Kazemi" <ramtin.kaz...@yahoo.com> wrote:
> Hi
> How can i perform bitwise rotation in C#?


Hi Ramtin,
You'll probably need to add some error checking & input validation,
but something like this seems to work:

int rotateBits = 3;
int dataSize = 16;
bool rotateleft = true; // false = right

uint value = 0xABCD;
uint mask = (uint)((1 << dataSize) - 1);
if (rotateleft)
value = ((value << rotateBits) | (value >> (dataSize - rotateBits)))
& mask;
else
value = ((value >> rotateBits) | (value << (dataSize - rotateBits)))
& mask;

John
 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      21st Nov 2007
On Nov 21, 8:17 am, "Ramtin Kazemi" <ramtin.kaz...@yahoo.com> wrote:
> Hi
> How can i perform bitwise rotation in C#?


This is one use case that should be implemented as a native BCL method
so that the JIT compiler could map it directly to a CPU instruction.
Another one I've been wanting is a function that counts the number set
bits in field.
 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      21st Nov 2007
On 21 Nov, 15:10, John Duval <JohnMDu...@gmail.com> wrote:
> On Nov 21, 9:17 am, "Ramtin Kazemi" <ramtin.kaz...@yahoo.com> wrote:
>
> > Hi
> > How can i perform bitwise rotation in C#?

>
> Hi Ramtin,
> You'll probably need to add some error checking & input validation,
> but something like this seems to work:
>
> int rotateBits = 3;
> int dataSize = 16;
> bool rotateleft = true; // false = right
>
> uint value = 0xABCD;
> uint mask = (uint)((1 << dataSize) - 1);
> if (rotateleft)
> value = ((value << rotateBits) | (value >> (dataSize - rotateBits)))
> & mask;
> else
> value = ((value >> rotateBits) | (value << (dataSize - rotateBits)))
> & mask;
>
> John


Ah yes I spotted my error as soon as I hit post. I just popped back
with a proper solution but you all beat me too it
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitwise [OR] David Microsoft C# .NET 4 12th Oct 2005 08:03 AM
Bitwise Or and bitwise shifting..... James Dean Microsoft C# .NET 5 22nd Jun 2004 11:29 AM
bitwise rotation Shawn B. Microsoft C# .NET 33 15th Oct 2003 07:24 PM
Bitwise Rotation II Shawn B. Microsoft C# .NET 0 13th Oct 2003 04:13 PM
Bitwise And Justin Starnes Microsoft Excel Programming 1 22nd Jul 2003 05:17 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:18 AM.