Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda