473,388 Members | 946 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,388 software developers and data experts.

bit rotation

Looking around in the class libraries there doesn't appear to be any methods
that do the equivalent of the C runtime lib functions rotr() and rotl(). I
wrote the following but then began to wonder if someone had some generic
methods that would handle differnet word sizes and and signed datatypes.

Doing bit shifting on the signed datatypes kind of surprised me originally.
I thought that if you shifted -2147483643 (0x80000005) by 8 bits right by
doing the folliowing:
-2147483643 >16
you would get 0x00058000 but you don't. Instead you get 0xffff8000 becuase
the shift keeps sign extending the thing. So I wrote my own signed int shift
like so:

public static int rotr(int val, int shift)
{
return (val & 0x7fffffff) >shift | val << (32 - shift) | ((val
< 0) ? 0x01 << (32 - shift - 1) : 0);
}
public static int rotl(int val, int shift)
{
return (val & 0x7fffffff) >(32 - shift) | val << shift | ((val
< 0) ? 0x01 << (shift - 1) : 0);
}
Where I strip off the high bit and handle it spearately.

Does anyone have any generic methods that do this for all integer sizes both
signed and unsigned? If so I would like to see them as I've spent a little
while trying and can't seem to get it. Or...if someone has a better way I'd
like to know that also...or if I just missed the implementation of these
things in the class lib.

thanks.

Jun 27 '08 #1
2 4027
Brian wrote:
Looking around in the class libraries there doesn't appear to be any methods
that do the equivalent of the C runtime lib functions rotr() and rotl().
Those are not standard functions, so it depends on what C runtime you're
talking about. :-)
I wrote the following but then began to wonder if someone had some
generic methods that would handle differnet word sizes and and signed
datatypes.
There's no real reason to worry too much: there are only three word sizes in
..NET, and rotating on signed types with preservation of the sign is unlikely
to be of any use.
Doing bit shifting on the signed datatypes kind of surprised me originally.
I thought that if you shifted -2147483643 (0x80000005) by 8 bits right by
doing the folliowing:
-2147483643 >16
That would be shifting by 16 bits.
you would get 0x00058000 but you don't.
Well, for that you'd actually need to *rotate* the value rather than shift it.

I have full confidence that you were exact to the computer, but still. :-)
Instead you get 0xffff8000 becuase the shift keeps sign extending the
thing. So I wrote my own signed int shift like so:

public static int rotr(int val, int shift)
{
return (val & 0x7fffffff) >shift | val << (32 - shift) | ((val
< 0) ? 0x01 << (32 - shift - 1) : 0);
}
public static int rotl(int val, int shift)
{
return (val & 0x7fffffff) >(32 - shift) | val << shift | ((val
< 0) ? 0x01 << (shift - 1) : 0);
}

Where I strip off the high bit and handle it spearately.
If your intention is to treat signed types as if they were unsigned, it's a
lot easier to just do that:

[CLSCompliant(false)]
public static uint RotateRight(uint value, int count) {
if (count < 0) throw new ArgumentOutOfRangeException("count");
count %= 32;
return (value >count) | (value << (32 - count));
}

public static int RotateRight(int value, int count) {
return unchecked((int) RotateRight((uint) value, count));
}

A few notes:
- The CLSCompliant attribute marks this method as not conforming to the
Common Language Specification, since you can't use it if your language has
no unsigned types. That's no problem since we provide an alternative with
only signed types. This attribute is only necessary if you're exporting this
function in a library.
- I choose to consider a negative count an error, but not count 32, since
it has a reasonable interpretation.
- The "unchecked" suppresses any integer overflow detection (if compiled
with this) for converting the values between signed and unsigned. We don't
need "unchecked" in the unsigned version because bit shifting never produces
overflows.
Does anyone have any generic methods that do this for all integer sizes both
signed and unsigned?
Well, there are only three differently-sized integral types in .NET (byte,
int, long) and unlike C their sizes are exact, so these overloads would be
fairly straightforward. You cannot write a single method that takes any
integer (unless you use Object, which would be bad), and while you could
write just the ulong method and have the rest delegate to it the added
complexity isn't worth it.

Also, operations on bytes are not that common since they're promoted to ints
by just about every operation, so you might not need rotation on bytes at all.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
Well, there are only three differently-sized integral types in .NET
(byte, int, long) and unlike C their sizes are exact, so these
Those are 8, 32, and 64 bits, respectively. You forgot short, which is 16
bits.

For such work it may be preferable to use the names System.Byte,
System.UInt16, System.UInt32, System.UInt64 which specifically call out the
number of bits (except for 8).
Jun 27 '08 #3

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

Similar topics

21
by: M. Clift | last post by:
Hi All, Could someone help me out with this? items = ('a', 'b', 'c', 'd') items + 1 = ( 'b', 'c', 'd', 'a') items + 2 = ( 'c', 'd', 'a', 'b') items + 3 = ( 'd', 'a', 'b', 'c') trans = 1
3
by: news | last post by:
Hi Guys, Sorry if this is an obvious question but Im trying to rotate a bitmap and then save it. So far Ive only found how to rotates a Graphic but I can't find how to save it. I also want to...
5
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft()...
2
by: the other john | last post by:
I am looking for ideas on how to create a rollover that triggers an image rotation in another location. I know how to create a function for basic rollovers, image swapping sort of thing, but I...
0
by: VorTechS | last post by:
I'm having a problem with an inherited label, applying text rotation to aligned text. If text rotation is applied to the aligned text, the alignment goes 'nuts'. I can find no logic to what is...
2
by: Roger Li | last post by:
I got a problem about the mean rotation calculation. I have a group of rotation matrics in my program and I want to calculate the mean rotation on the basis of these rotation matrics. I think it...
6
by: Ramtin Kazemi | last post by:
Hi How can i perform bitwise rotation in C#?
3
by: kimiraikkonen | last post by:
Hi, Here is a very interesting issue, Assume i have picturebox and loaded a image into picturebox, however whenever i want to rotate image using: 'For example 90 degree...
6
by: elrondrules | last post by:
I want to create a simple log rotation for the following scenario: I have a script that when executed will write debug and error messages to a specific file (say script.log). I want to create...
8
by: infoseekar | last post by:
Image Resize & Rotation Hi I have 2 scripts, one for Image rotation and other image resize and they both are working. Image resize scripts load the picture and resize it and Image rotation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.