function base64UrlToBits(value) {
  const base64 = value
    .replace(/-/g, "+")
    .replace(/_/g, "/")
    .padEnd(Math.ceil(value.length / 4) * 4, "=");

  const binary = atob(base64);
  let bits = "";

  for (let index = 0; index < binary.length; index += 1) {
    bits += binary
      .charCodeAt(index)
      .toString(2)
      .padStart(8, "0");
  }

  return bits;
}

function bitsToInt(bits, start, length) {
  return parseInt(bits.slice(start, start + length), 2);
}

function bitsToBoolean(bits, index) {
  return bits[index] === "1";
}
