max xor

Benchmark created on


Setup

let k = 4570;
let lo = 50;
let hi = 501;

Test runner

Ready to run.

Testing in
TestOps/sec
inverted
function maxXor(lo, hi, k) {
	while (k > 0) {
		for (let i = lo; i <= hi; ++i) {
			if (((i ^ k) <= hi) && ((i ^ k) >= lo)) {
				return k;
			}
		}
		--k;
	}
	return 0;
}
maxXor(lo, hi, k);
ready
traditional

function maxXor(lo,hi,k) {
	let max = 0;
	for (let i = lo; i <= hi; ++i) {
	  for (let j = lo+1; j <= hi; ++j) {
		let x = i ^ j;
        if ((x <= k) && (x > max)) {
           max = x;
        }
      }
    }
  return max;
}
maxXor(lo,hi,k);
ready
with optimized k
function maxXor(lo, hi, badK) {
	let k = 0;
	if (badK > hi) {
		badK = hi;
		while (badK > 0) {
			k <<= 1;
			k |= 1;
			badK >>= 1;
		}
	} else {
		k = badK;
	}
	while (k > 0) {
		for (let i = lo; i <= hi; ++i) {
			if (((i ^ k) <= hi) && ((i ^ k) >= lo)) {
				return k;
			}
		}
		--k;
	}
	return 0;
}
maxXor(lo, hi, k);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.