fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. long y = scanner.nextLong();
  8. long k = scanner.nextLong();
  9.  
  10. long notPossible = 0;
  11. long res = 0;
  12.  
  13. for(long i = 10; i>=0; i--){
  14. long initial_num = 0;
  15. //get digit at current iteration in bin num
  16. long g = y>>i;
  17. if ((g & 1) == 1){
  18. initial_num = 1;
  19. }
  20. else{
  21. initial_num = 0;
  22. }
  23. long final_num = 0;
  24. g = k>>i;
  25. if ((g & 1) == 1){
  26. final_num = 1;
  27. }
  28. else{
  29. final_num = 0;
  30. }
  31. if ( initial_num == final_num ){
  32. //then do nothing.. the res is anyway going to be 0 at i
  33. }
  34. else{
  35. if ( initial_num == 0){
  36. //then we need 1 at the ith pos of the res which we will || with to get final
  37. // this case means that initial != final at i and initial doesnt have 1 which final has.. so
  38. // num we OR with needs to have a 1
  39. res |= (1L << i);
  40. // left shift because i want to place 1 at a specific pos
  41. //LEFT SHIFT = “move 1 to position i”
  42. /*
  43.   Right shift does the opposite: 1000 >> 1 = 0100
  44.   👉 It loses information (moves bits away)
  45.   8*/
  46. }
  47. else{
  48. notPossible = -1;break;
  49. }
  50. }
  51. }
  52. if (notPossible == -1) {
  53. System.out.println(notPossible);
  54. } else {
  55. System.out.println(res);
  56. }
  57. }
  58. }
  59.  
Success #stdin #stdout 0.15s 54524KB
stdin
10 15
stdout
5