Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda