3

I am trying to find a way to reverse a number without

  1. Converting it to a string to find the length
  2. Reversing the string and parsing it back
  3. Running a separate loop to compute the Length

i am currently doing it this way

 public static int getReverse(int num){
        int revnum =0;
        for( int i = Integer.toString(num).length() - 1 ; num>0 ; i-- ){
            revnum += num % 10 * Math.pow( 10 , i );
            num /= 10;
        }
        return revnum;        
    }

But I would Like to implement the above 3 conditions.

I am looking for a way , possibly using the bit wise shift operators or some other kind of bitwise operation.

Is it possible ? If so how ?

PS : If 1234 is given as input it should return 4321. I will only be reversing Integers and Longs

Gautam
  • 7,868
  • 12
  • 64
  • 105

3 Answers3

7

How about:

int revnum = 0;
while (num != 0) {
  revnum = revnum * 10 + (num % 10);
  num /= 10;
}
return revnum;

The code expects a non-negative input.

This may or may not matter to you, but it's worth noting that getReverse(getReverse(x)) does not necessarily equal x as it won't preserve trailing zeroes.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    Its ok If it doesn't reverse negative numbers , I'll just convert it into positive and reverse it and then convert it back to negative – Gautam Sep 19 '11 at 15:04
  • I guess `getReverse(getReverse(x))` won't be much of a problem now , but if it does I might have to rethink what I am using this function for. I think I will be getting the same problem even with the previous implementation – Gautam Sep 19 '11 at 15:17
  • @gautham5678: That's correct: this is a feature of the function's signature (`int`->`int`) rather than of this implementation. – NPE Sep 19 '11 at 15:18
2

How about this? It handles negative numbers as well.

public int getReverse(int num){
   int rst=0;
   int sign;
   sign=num>0?1:-1;

   num*=sign;
   while(num>0){
      int lastNum = num%10;
      rst=rst*10+lastNum
      num=num/10;
   }
   return rst*sign;
}
dreamlax
  • 93,976
  • 29
  • 161
  • 209
Ivan
  • 766
  • 6
  • 17
0

Integer.MAX_VALUE or Integer.MIN_VALUE is not at all considered in any of the solutions.

For eg: if the input is -2147483647 or 2147483647 the o/p will be 1126087180 and -1126087180 respectively.

Please try the below solutions where both the conditions are handled, so if at any point of time the number is going beyond the boundary conditions i.e., INPUT_VALUE > Integer.MAX_VALUE or INPUT_VALUE < Integer.MIN_VALUE it would return 0

class ReversingIntegerNumber {
    
    public int reverse(int originalNum) {
        boolean originalIsNegative = originalNum > 0 ? false : true;
        int reverseNum = 0;
        int modValue;
        originalNum = Math.abs(originalNum);
        while(originalNum != 0) {
            modValue = originalNum % 10;
            if(reverseNum > (Integer.MAX_VALUE - modValue)/10) {
                return 0;
            }
            reverseNum = (reverseNum * 10) + modValue;
            originalNum /= 10;
        }
        return originalIsNegative ? -1 * reverseNum : reverseNum;
    }
}
Koushlendra
  • 280
  • 1
  • 9