Results 1 to 3 of 3
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Flip int value(1234 becomes 4321)

    Is there a fast way to flip an int? For example:

    Java Code:
    int i = 1234;
    System.out.println(flipInt(i)); //4321
    This could be done by converting it to a string and reverse it. But it is a very inefficient and expensive way to do it. Is there any faster way?
    Like with bit shifting?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Flip int value(1234 becomes 4321)

    Look at using the %, /, + & * operators
    Last edited by Norm; 09-28-2012 at 12:47 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Flip int value(1234 becomes 4321)

    Careful though.

    What is the result of flipping 1230?

    what the result from the flip above -- what about 321?
    Should it result in 123? 1230? 12300?

Similar Threads

  1. Flip Pictures on DrJava
    By siddanth1 in forum New To Java
    Replies: 2
    Last Post: 02-10-2012, 03:08 AM
  2. coin flip program
    By chandrasekhar.melam in forum New To Java
    Replies: 5
    Last Post: 09-26-2011, 06:30 PM
  3. How to Copy and Flip a Picture Horizontally
    By tecnicamente in forum New To Java
    Replies: 1
    Last Post: 05-11-2011, 02:47 AM
  4. Othello/Reversi flip checkers
    By chielt in forum Java Applets
    Replies: 10
    Last Post: 01-03-2010, 12:25 AM
  5. Random coin flip application
    By Boomer1 in forum New To Java
    Replies: 8
    Last Post: 12-18-2009, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •