1

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#.

Tennant125
  • 37
  • 4
  • 3
    You 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
  • 1
    It'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
  • @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 Answers5

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,014
  • 13
  • 41
  • 62
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

A bit late, but this is how I solved it:

public static long Reverse(long i) =>
    BitConverter.ToInt64(BitWiseReverseByteArray(BitConverter.GetBytes(i)).ToArray());

private static IEnumerable<byte> BitWiseReverseByteArray(IEnumerable<byte> array) =>
    array.Select(b => BitWiseReverseByte(b)).Reverse();

private static byte BitWiseReverseByte(byte b)
{
    b = (byte) ((b & 0x55) << 1 | (b >> 1) & 0x55);
    b = (byte) ((b & 0x33) << 2 | (b >> 2) & 0x33);
    b = (byte) ((b & 0x0F) << 4 | (b >> 4) & 0x0F);
    return b;
}
Andie2302
  • 4,741
  • 3
  • 21
  • 39
-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,339
  • 6
  • 37
  • 47
-2

Hope this will work

string s = 101010100000001.tostring();
char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
BJ Myers
  • 6,339
  • 6
  • 37
  • 47