Tennant125
Tennant125

Reputation: 33

Reversing a long in c#

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

Upvotes: -1

Views: 2134

Answers (6)

Graham Epps
Graham Epps

Reputation: 101

The original question asked for reversal of the binary bit, not the decimal digits. The following will perform a bit-wise reversal of the bits in a uint. To change it to a ulong: s/uint/ulong/g s/31/63/g s/32/64/g

// Reverse the bits in a uint
uint ReverseBits(uint x)
{
    uint ret = 0;
    uint maskI = (uint)1;         // lsb
    uint maskO = (uint)1 << 31;   // msb

    for (int i = 0; i < 32; i++)
    {
        // If the specified bit is set, then set the corresponding bit in the return result.
        if ((x & maskI) != 0)
            ret |= maskO;

        maskI = maskI << 1;
        maskO = maskO >> 1;
    }

    return ret;
}

Upvotes: 0

Andie2302
Andie2302

Reputation: 4887

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;
    }

Upvotes: 0

Craig
Craig

Reputation: 12012

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);
    }

Upvotes: 2

Rajesh Varma
Rajesh Varma

Reputation: 47

Hope this will work

string s = 101010100000001.tostring();
char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );

Upvotes: -1

MariusUt
MariusUt

Reputation: 752

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;
}

Upvotes: 1

BJ Myers
BJ Myers

Reputation: 6813

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();

Upvotes: -1

Related Questions