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

176
Visualizações
Bitwise operator for modulo and rounding

I have a function which takes a fractional number, and converts it to an integer combined with a binary exponent.

For example, get_whole_number(10.5) will return => 21, 1, where

  • 21 is the integer
  • 1 is the power of 2 you must divide the 21 by to get the original float number (21/(2**1) = 10.5).

So far I have this function, and it works but it is inefficient and slow:

function get_whole_number(number, exponent=0) {
    if(number%1 == 0) return [number, exponent];
    exponent++;
    number *= 2;
    if(exponent >= 500) return [Math.round(number), exponent];
    return get_whole_number(number, exponent);
};

console.log(
  get_whole_number(1.125).join("/2^"));

To make it faster, I wanted to replace some operations, notably number%1 and Math.round with bitwise operations.

So my question is, how do I go about replacing % 1 and round() with bitwise operators?

I was thinking that I could use:

  • if((number & number) == number) instead of if(number%1 == 0) because bitwise operations ignore the fractional part of a number; but this only works sometimes.
  • (number | number) instead of Math.round(number) but this just truncates the value.
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