Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1549 | Anzahl registrierter User: 1833 | Besucher online: 565
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Integer Rotate Left/Right


Autor: Alexander Schuc
Sprache: C#
Bewertung: 4,2
(1 Bewertung)
Anzahl der Aufrufe: 5621
  
Kick it on dotnet-kicks.de  

Beschreibung:

Einfache Umsetzung eines Bit-Rotate für Integer-Werte.

Kann leicht für andere Datentypen (long, byte) umgeschrieben werden.


Abgelegt unter: rotate left, rotate right, bit operation, integer.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public static class Utils
{
	/// <summary>
	/// Rotates the given int value right by the specified number of bits.
	/// </summary>
	/// <param name="number">The integer to rotate</param>
	/// <param name="distance">The number of bits to rotate</param>
	/// <returns>Returns the given int rotated right side by the given distance</returns>
	public static int RotateRight(int i, int distance)
	{
		uint num = (uint)i;
		int length = (sizeof(int) * 8);
		distance = distance % length;
		uint add = num << (length - distance);
		num = num >> distance;
		num = num | add;
		return (int)num;
	}

	/// <summary>
	/// Rotates the given int value left by the specified number of bits.
	/// </summary>
	/// <param name="number">The integer to rotate</param>
	/// <param name="distance">The number of bits to rotate</param>
	/// <returns>Returns the given int rotated left side by the given distance</returns>
	public static int RotateLeft(int i, int distance)
	{
		uint num = (uint)i;
		int length = (sizeof(int) * 8);
		distance = distance % length;
		uint add = num >> (length - distance);
		num = num << distance;
		num = num | add;
		return (int)num;
	}
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

v.wochnik schrieb am:  24.12.2007 00:08:19

Coole sache, aber was soll man denn damit machen?
Alexander Schuc schrieb am:  24.12.2007 13:04:16

Vielleicht brauchts mal ein Algorithmus denn du implementieren musst. :)


schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.