Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Rotate bits to left overflow issue

Tags:

c#

bit

I've been trying to get this to work for several days now, I've read a thousand guides and people's questions, but still, I can't find a way to do it properly.

What I want to do is to rotate the bits to the left, here's an example.

Original number = 10000001 = 129
What I need = 00000011 = 3

I have to rotate the bits to left a certain amount of times (it depends on what the user types), here's what I did:

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

Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);

The issue with this it that it causes an error (OverflowException) when I try to use the (<<) operator with that number (note that if I put a number which first bit is a 0; example: 3 = 00000011; it works as intended and it returns a 6 as a result.

The problem is, if the first bit is a 1, it gives me the (OverflowException) error. I know this isn't rotating, its just a shifting, the first bit goes away and on the end of the byte a 0 pops up, and I can then change it with an OR 000000001 operation to make it a 1 (if the first bit was a 1, if it was a 0 I just leave it there).

like image 286
Lobialkon Avatar asked Apr 25 '14 14:04

Lobialkon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You're getting an overflow exception because you're operating in a checked context, apparently.

You can get around that by putting the code in an unchecked context - or just by making sure you don't perform the cast back to byte on a value that can be more than 255. For example:

int shifted = b << rotateLeftBits;
int highBits = shifted & 0xff;
int lowBits = shifted >> 8; // Previously high bits, rotated
byte result = (byte) (highBits | lowBits);

This will work for rotate sizes of up to 8. For greater sizes, just use rotateLeftBits % 8 (and normalize to a non-negative number if you might sometimes want to rotate right).

like image 187
Jon Skeet Avatar answered Sep 27 '22 23:09

Jon Skeet