473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

bitwise rotation in c#

Hi
How can i perform bitwise rotation in C#?
Nov 21 '07 #1
6 7179
On 21 Nov, 14:17, "Ramtin Kazemi" <ramtin.kaz...@yahoo.comwrote:
Hi
How can i perform bitwise rotation in C#?
int x = 1;
x<<=3;
Console.WriteLine(x.ToString()); //writes 8
Nov 21 '07 #2
DeveloperX wrote:
On 21 Nov, 14:17, "Ramtin Kazemi" <ramtin.kaz...@yahoo.comwrote:
>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
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
Nov 21 '07 #3
On Nov 21, 9:17 am, "Ramtin Kazemi" <ramtin.kaz...@yahoo.comwrote:
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
Nov 21 '07 #4
DeveloperX wrote:
On 21 Nov, 14:17, "Ramtin Kazemi" <ramtin.kaz...@yahoo.comwrote:
>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
Nov 21 '07 #5
On Nov 21, 8:17 am, "Ramtin Kazemi" <ramtin.kaz...@yahoo.comwrote:
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.
Nov 21 '07 #6
On 21 Nov, 15:10, John Duval <JohnMDu...@gmail.comwrote:
On Nov 21, 9:17 am, "Ramtin Kazemi" <ramtin.kaz...@yahoo.comwrote:
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 :)
Nov 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Shawn B. | last post by:
Greetings, I am simulating an assembly language bit rotation in C# and it works wonderfully --------- .... public uint Value; .... public uint RotateRight(byte count) {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.