0

In JavaScript, how can I know if a single Bit is On (1) or Off (0)?

function isBitOn(number, index)
{
  // ... ?
}

// Example:
let num = 13; // 1101

isBitOn(num, 0); // true   1
isBitOn(num, 1); // false  0
isBitOn(num, 2); // true   1
isBitOn(num, 3); // true   1

I know that in JavaScript we have Bitwise Operations. But How can I use them, or use any other method, the read single Bit?

1

2 Answers 2

3

JavaScript retains the legacy of the C binary operators. You use the "index" to shift the value 1 to the left, and then you AND the result to the number you're interested in:

function isBitOn(number, index) {
  return Boolean(number & (1 << index));
}
console.log(isBitOn(13, 0));
console.log(isBitOn(13, 1));
console.log(isBitOn(13, 2));
console.log(isBitOn(13, 3));
console.log(isBitOn(13, 4));
console.log(isBitOn(13, 5));
console.log(isBitOn(13, 6));

Note that that's the bitwise & operator, and not the && short-circuit logical operator.

Also note that those operators treat the numbers as 32-bit integers, so it can't work for very large numbers.

5
  • What am I missing? lets test num = 13 (13 is 1101) and index = 2. So (1 << 2) is 4 (4 is 0100). Now lets look at (1101 & 0100) is 0100. SO why when I run it I get 1 and not 0100. please help me understand Jun 7, 2020 at 19:13
  • Are you running the current version? It converts the computed result to a boolean before returning. Any non-zero number when converted to boolean is true
    – Pointy
    Jun 7, 2020 at 19:30
  • If you run the version without that change, you get 1 0 4 8 0 0
    – Pointy
    Jun 7, 2020 at 19:31
  • ok, this is a simple number to boolean conversion. I was thinking I'm missing something. Jun 8, 2020 at 8:18
  • 1
    @GilEpshtain To get 0 and 1, you'd use (number >>> index) & 1
    – Bergi
    Jun 8, 2020 at 13:40
2

Convert the number to binary, then check the bits:

function isBitOn(number, index) {
  let binary = number.toString(2);
  return (binary[(binary.length - 1) - index] == "1"); // index backwards
}

let num = 13; // 1101

console.log(isBitOn(num, 0));
console.log(isBitOn(num, 1));
console.log(isBitOn(num, 2));
console.log(isBitOn(num, 3));

9
  • This will not work unless you expect bits to be numbered from the most-significant to the least significant. Also if the input number is not an integer the resulting string from calling .toString(2) will contain a "." character.
    – Pointy
    Jun 7, 2020 at 14:44
  • @GilEpshtain note that it produces the wrong results.
    – Pointy
    Jun 7, 2020 at 14:44
  • @Pointy the most-significant bit is always at the start of the resulting string. And yes, it won't work with decimals, but the result for the 13 provided in the question is correct.
    – Wais Kamal
    Jun 7, 2020 at 14:46
  • 1
    @WaisKamal but note the expected results in the OP. Usually, in modern computing people expect bit 0 to be the least significant bit.
    – Pointy
    Jun 7, 2020 at 14:46
  • @Pointy do I get from you that the OP wants the bits from the right to left?
    – Wais Kamal
    Jun 7, 2020 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.