Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Setting bits on an integer and displaying it on the page

I'm trying to set 4 bits on an integer based on whether a checkbox is checked. I'm new with JavaScript so pardon my bad code. I searched how to set bits and found the function setBit. It's clear what it does, however when I print out the result n, I keep on getting 0. I'm sure I'm doing something wrong, but I can't quite figure out what it is.

Any help is welcomed.

function setBit(number, bitPosition) {
  return number |= 1 << bitPosition;
}

function checkCheckbox() {
  var one = document.getElementById("one");
  var two = document.getElementById("two");
  var three = document.getElementById("three");
  var four = document.getElementById("four");
  var n = 0

  if (one.checked == true) {
    setBit(n, 0)
  }

  if (two.checked == true) {
    setBit(n, 1)
  }

  if (three.checked == true) {
    setBit(n, 2)
  }

  if (four.checked == true) {
    setBit(n, 3)
  }

  return document.getElementById("out").innerHTML = n;

}
<h2>Numbers</h2>
<br>
<p>
  1? <input type="checkbox" id="one" value="1"><br>
  2? <input type="checkbox" id="two" value="2"><br>
  3? <input type="checkbox" id="three" value="3"><br>
  4? <input type="checkbox" id="four" value="4">
</p>
<br> <br>
<button onclick="checkCheckbox()">Submit</button> <br>

<h3 style="color:green" id="out"></h3>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!