4 Replies Latest reply: Feb 14, 2007 6:26 AM by 791266 RSS

    About Integer.reverse()

    807599
      Hi

      How Integer.reverse() method working
      expecting detail explanation


      regards
        • 1. Re: About Integer.reverse()
          807599
          int java.lang.Integer.reverse(int i)

          reverse
          public static int reverse(int i)
          Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.

          Returns:
          the value obtained by reversing order of the bits in the specified int value.

          ** that's from the documentation

          from what I understand, for example, I have
          int number = 1;
          the binary representation of 1 in two's complement is

          0000 0000 0000 0000 0000 0000 0000 0001

          (since int is 32 bits)

          if we do
          System.out.println(Integer.reverse(number));
          what I got as result is -2147483648. Probably what it did was to reverse the order of the bits (not the value), hence the binary value became

          1000 0000 0000 0000 0000 0000 0000 0000 (this is reversing the order of the bits)

          1111 1111 1111 1111 1111 1111 1111 1110 (this is reversing the value of the bits, and this is NOT what Integer.reverse() does)

          correct me if I'm wrong guys..
          • 2. Re: About Integer.reverse()
            807599
            Hello,

            Check the API - http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html

            From the explanation of the API, 2 (00000000000000000000000000000010 in binary) will become 1073741824 (01000000000000000000000000000000 in binary) unless I don't understand the API correct.
            • 3. Re: About Integer.reverse()
              807599
              Hello,

              Check the API -
              http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Inte
              ger.html

              From the explanation of the API, 2
              (00000000000000000000000000000010 in binary) will
              become 1073741824 (01000000000000000000000000000000
              in binary) unless I don't understand the API correct.
              This is correct. first our Integer value converted into binary value. then reverse that binary & calculates the decimal value.
              it will gives the int value.

              if our Integer value is odd number, the return unt value is -ve.
              if our Integer value is even number, the return unt value is +ve.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
              • 4. Re: About Integer.reverse()
                791266
                This is correct.
                first our Integer value converted into binary value.
                All values are binary so there is no conversion.

                Kaj