Problem:
Score : 900 points

Problem Statement

Nukes has an integer that can be represented as the bitwise OR of one or more integers between A and B (inclusive).
How many possible candidates of the value of Nukes's integer there are?

Constraints

1 ≤ A ≤ B < 2^{60}

A and B are integers.

Input

The input is given from Standard Input in the following format:

A
B

Output

Print the number of possible candidates of the value of Nukes's integer.

Sample Input 1

7
9

Sample Output 1

4

In this case, A=7 and B=9. There are four integers that can be represented as the bitwise OR of a non-empty subset of {7, 8, 9}: 7, 8, 9 and 15.

Sample Input 2

65
98

Sample Output 2

63

Sample Input 3

271828182845904523
314159265358979323

Sample Output 3

68833183630578410

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io;
use std::cmp::max;
use std::collections::VecDeque;

fn main() {
    let mut input = Input::new();
    let a = input.next().parse::<i64>().unwrap();
    let b = input.next().parse::<i64>().unwrap();
    println!("{}", test(a, b));
}

fn solve(a_: i64, b_: i64) -> i64 {
    let mut a = a_;
    let mut b = b_;
    if a == b {
        return 1;
    }
    let c = a ^ b;
    let r = (0..62).filter(|x| ((1i64 << x) & c) > 0i64).max().unwrap();
    let t = 1i64 << r;
    a &= t - 1;
    b &= t - 1;
    let s1 = a;
    let l1 = t;
    let s3 = t + a;
    let l3 = t << 1;
    let mut x: i64 = l1 - s1 + l3 - s3;
    if b > 0 {
        let s2 = t;
        let l2 = t + (2i64 << (0..62).filter(|x| ((1i64 << x) & b) > 0i64).max().unwrap());
        x += l2 - s2 - max(l2 - s3, 0);
    }
    return x;
}

fn test(a_: i64, b_: i64) -> i64 {
    let mut a = a_;
    let mut b = b_;
    if a == b {
        return 1;
    }
    let c = a ^ b;
    let r = (0..62).filter(|x| ((1i64 << x) & c) > 0i64).max().unwrap();
    let t = 1i64 << r;
    a &= t - 1;
    b &= t - 1;
    let u = (0..62).filter(|x| (1i64 << x) > b).min().unwrap();
    let mi = a;
    let ma = 1i64 << u;
    return t + t - mi + if ma > mi { 0 } else { ma - mi };
}

struct Input {
    queue: VecDeque<String>,
}

impl Input {
    fn new() -> Input {
        Input { queue: VecDeque::new() }
    }

    fn next(&mut self) -> String {
        if self.queue.is_empty() {
            let mut line = String::new();
            let _ = io::stdin().read_line(&mut line);
            for x in line.split_whitespace() {
                self.queue.push_back(String::from(x));
            }
        }
        return self.queue.pop_front().unwrap();
    }
}