Problem: 2057C - Trip to the Olympiad
Language: Java 8

Previous submission (verdict: Wrong answer on test 2):
import java.util.*;

public class C{
    public static boolean powerOf2(int n){
        int a = 1;
        while(a<n) a = a*2;
        if(a==n) return true;
        return false;
    }
    public static void main(String...args){
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        while(t>0){
            t--;

            int l = sc.nextInt();
            int r = sc.nextInt();

            int mid = 0;
            boolean smallerThanr = false, greaterThanl = false;

            int start = 30;
            for(int i=30; i>=0; i--){
                int rbit = (((1<<i)&r)>>i);
                if(rbit==1){
                    start = i;
                    break;
                }
            }

            if(powerOf2(r)){
                mid = 0;
            }
            else if(powerOf2(l+1)){
                mid += 1<<start;
            }
            else{
                start = 30;
            }

            for(int i=start; i>=0; i--){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                int mBit = 0;

                if(lbit==1 && rbit == 1 ){
                    if(!smallerThanr){
                        mBit = 1;
                    } // else 0
                }
                else if(lbit == 0 && rbit == 0){
                    if(!smallerThanr){
                        mBit = 0;
                    }
                    else if(!greaterThanl){
                        mBit = 1;
                    } //else 0
                }
                else if(lbit == 0 && rbit == 1){
                    if(!smallerThanr){
                        mBit = 0;
                        smallerThanr = true;
                    }
                    else if(!greaterThanl){
                        mBit = 1;
                        greaterThanl = true;
                    }
                }
                else{ // lbit == 1 && rbit == 0
                    if(!greaterThanl){
                        mBit = 1;
                    }
                }

                mid += (mBit<<i);
            }

            System.out.println(l + " " + mid + " " + r);
        }
    }
}

Submission (verdict: Wrong answer on test 2):
import java.util.*;

public class C{
    public static boolean powerOf2(int n){
        if(n==1) return false;
        int a = 1;
        while(a<n) a = a*2;
        if(a==n) return true;
        return false;
    }
    public static void main(String...args){
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        while(t>0){
            t--;

            int l = sc.nextInt();
            int r = sc.nextInt();

            int mid = 0;
            boolean smallerThanr = false, greaterThanl = false;

            int start = 30;
            for(int i=30; i>=0; i--){
                int rbit = (((1<<i)&r)>>i);
                if(rbit==1){
                    start = i;
                    break;
                }
            }

            if(powerOf2(r)){
                mid = 0;
            }
            else if(powerOf2(l+1)){
                mid += 1<<start;
                greaterThanl = true;
            }
            else{
                start = 30;
            }

            for(int i=start; i>=0; i--){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                int mBit = 0;

                if(lbit==1 && rbit == 1 ){
                    if(!smallerThanr){
                        mBit = 1;
                    } // else 0
                }
                else if(lbit == 0 && rbit == 0){
                    if(!smallerThanr){
                        mBit = 0;
                    }
                    else if(!greaterThanl){
                        mBit = 1;
                    } //else 0
                }
                else if(lbit == 0 && rbit == 1){
                    if(!smallerThanr){
                        mBit = 0;
                        smallerThanr = true;
                    }
                    else if(!greaterThanl){
                        mBit = 1;
                        greaterThanl = true;
                    }
                }
                else{ // lbit == 1 && rbit == 0
                    if(!greaterThanl){
                        mBit = 1;
                    }
                }

                mid += (mBit<<i);
            }

            System.out.println(l + " " + mid + " " + r);
        }
    }
}