fork download
  1. public class Main {
  2.  
  3. private static final int NORTH = 0x11111111; // 0001 0001 0001 ...
  4. private static final int EAST = 0x22222222; // 0010 0010 ...
  5. private static final int SOUTH = 0x44444444; // 0100 0100 ...
  6. private static final int WEST = 0x88888888; // 1000 1000 ...
  7.  
  8. static int leftOf(int direction) {
  9. return Integer.rotateLeft(direction, 1);
  10. }
  11.  
  12. static int rightOf(int direction) {
  13. return Integer.rotateRight(direction, 1);
  14. }
  15.  
  16. static String getName(int dir) {
  17. switch (dir) {
  18. case NORTH:
  19. return "North";
  20. case EAST:
  21. return "East";
  22. case SOUTH:
  23. return "South";
  24. case WEST:
  25. return "West";
  26. default:
  27. return "Error";
  28. }
  29. }
  30.  
  31. static final int[] TESTCASES = {
  32. NORTH,
  33. EAST,
  34. SOUTH,
  35. WEST
  36. };
  37.  
  38. public static void main(String[] args) {
  39. for (int test : TESTCASES) {
  40. System.out.printf("left: %5s self: %5s right:%5s%n",
  41. getName(leftOf(test)),
  42. getName(test),
  43. getName(rightOf(test))
  44. );
  45. }
  46. }
  47.  
  48. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
left:  East   self: North   right: West
left: South   self:  East   right:North
left:  West   self: South   right: East
left: North   self:  West   right:South