I know in java that you can simply reverse a long (101010100000001) by using long.reverse (100000001010101). However, is there anything like these that exists in c#.
Asked
Active
Viewed 1,781 times
-1
-
3You can reverse the bytes by calling BitConverter.GetBytes, Array.Reverse, then BitConverter.ToInt64. See the MSDN description of BitConverter for an example: https://msdn.microsoft.com/en-us/library/system.bitconverter(v=vs.110).aspx – Joe Mar 25 '15 at 17:43
-
Could you update your post to clarify exactly what you want to reverse? It seemed to me as you want to reverse the order of the _bits_, but some of the answers here seem to answer how to reverse the _bytes_. – MariusUt Mar 25 '15 at 17:47
-
@GrantWinney You should post your comment as an answer. Even if it's not the option for OP, it might be for someone else with a similar problem in the future. – Kapol Mar 25 '15 at 17:48
-
[Bit Twiddling Hacks](https://graphics.stanford.edu/~seander/bithacks.html) – EZI Mar 25 '15 at 17:53
-
Java's `Long.reverse` (mentioned by OP) reverses the bits exactly as in the OP's example. `Long.reverseBytes` reverses the bytes. Why are they being downvoted? The 3 reasons given to close are bogus. – Daniel Earwicker Mar 25 '15 at 18:08
-
@DanielEarwicker My understanding from this question is **a)** *Find me docs to do the same in c#* OR **b)** *Write it for me I am too lazy to research* , So this is why I downvoted. – EZI Mar 25 '15 at 19:10
-
@EZI - I wonder what you think the purpose of Stack Overflow is? – Daniel Earwicker Mar 25 '15 at 19:31
-
@DanielEarwicker I would expect some research and code. Otherwise, this site would be *HireACoder.com* – EZI Mar 25 '15 at 19:32
-
1It's a Q&A site. It's perfectly valid for someone to ask a simple question, especially if they go to the effort of clarifying exactly what they mean as the OP did here. The whole purpose of SO is to become a repository of such clear, straightforward questions and their answers, so others can find them in the future by googling. Please stop voting down valid questions. – Daniel Earwicker Mar 25 '15 at 19:39
-
1@DanielEarwicker Your statements like `It's perfectly valid` doesn't make it valid. `Please stop voting down valid questions` I'll continue to do it if I think a question is not valid for SO. Understood? – EZI Mar 25 '15 at 19:42
-
Of course you will. All I can do is explain to you that you've misunderstood the purpose of a Q&A site, and hope that you'll eventually become bored of your incredible power to downvote questions. – Daniel Earwicker Mar 25 '15 at 19:59
5 Answers
2
The answer to your question is no. However it is achievable by code. How about this...
public static long RevLong(long l)
{
long tmp = l;
long r = 0L;
if (tmp < 0)
tmp *= -1;
while (tmp > 0)
{
r = (r * 10) + (tmp - ((tmp / 10)) * 10);
tmp = tmp / 10;
}
return r * (l < 0 ? -1 : 1);
}

Craig
- 11,614
- 13
- 44
- 62
-
1this is slower than making long a string and reverse and parse back to long, I tested with this answer https://stackoverflow.com/a/29263007/1818723 and parsed back to long, and on 1M iterations I have x2 diff. Long to str to long took 00:00:00.1527619 and your solution 00:00:00.3237875 – Pawel Cioch Nov 21 '22 at 17:53
-
1
How about...
public ulong Bit(ulong x, int n)
{
return (x & (1 << n)) >> n;
}
public ulong ReverseBits(ulong x)
{
ulong result = 0;
for (int i = 0; i < 64; i++)
result = result | (x.Bit(64 - i) << i);
return result;
}

MariusUt
- 752
- 4
- 15
0
Another aproach for reversing a long is:
long num = 123456789;
long reversed = 0;
while (num > 0)
{
reversed = (reversed * 10) + (num % 10);
num /= 10;
}
or
long num = 123456789;
long reversed = 0;
while (num > 0)
{
reversed = (reversed << 1) + (reversed << 3) + (num & 1);
num >>= 1;
}
or
public static long ReverseBits(long number)
{
long result = 0;
const int numbersOfBitsInLong = sizeof(long) * 8; //Could be set to 64
for (var i = 0; i < numbersOfBitsInLong; i++)
{
result <<= 1;
result |= number & 1;
number >>= 1;
}
return result;
}

Andie2302
- 4,825
- 4
- 24
- 43
-1
There are some interesting examples here. You could adapt one of these into an extension method, like so:
public static class LongExtension
{
public static ulong Reverse(this ulong value)
{
return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 |
(value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 |
(value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 |
(value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56;
}
}
Then you can call it like this:
ulong myLong = 3L;
ulong reversed = myLong.Reverse();

BJ Myers
- 6,617
- 6
- 34
- 50
-1
Hope this will work
string s = 101010100000001.tostring();
char[] charArray = s.ToCharArray();
Array.Reverse( charArray );
return new string( charArray );

BJ Myers
- 6,617
- 6
- 34
- 50

Rajesh Varma
- 47
- 3