fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. private static int reverseBits(int value) {
  12. return value ^ ((Integer.highestOneBit(value) << 1) - 1);
  13. }
  14.  
  15. private static void test(int testId, int input, int expected) {
  16. int actual = reverseBits(input);
  17. if (expected == actual) {
  18. System.out.printf("Test %d OK%n", testId);
  19. } else {
  20. System.out.printf("Test %d failed, expected: %d, actual: %d%n", testId, expected, actual);
  21. }
  22. }
  23.  
  24. public static void main (String[] args) throws java.lang.Exception
  25. {
  26. test(1, 10, 5);
  27. test(2, 0xf0, 0x0f);
  28. test(3, 0xff0, 0x00f);
  29. test(4, 1, 0);
  30. test(5, 2, 1);
  31. test(6, 8, 7);
  32. }
  33. }
Success #stdin #stdout 0.08s 48692KB
stdin
Standard input is empty
stdout
Test 1 OK
Test 2 OK
Test 3 OK
Test 4 OK
Test 5 OK
Test 6 OK