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

229
Visualizações
bit-shift right-hand operand type

I'm wondering what the correct right-hand operand is for C/C++ bit-shift operators.

At time of writing, the built-in arithmetic types are all less than 256 bits, so a single byte would be sufficient. Furthermore, x86 shift-instructions use imm8. Together this suggests the right-hand operand should be an unsigned char and use of a different type here will require type-conversion.

Is there a "most correct" type to use here? I know the standard is strangely lenient about other aspects of bit-shifting so maybe this is another case of the same?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Any integer type can be used as the right operand of a bitwise shift, so long as the value is at least 0 and less than the bit length of the left operand.

This is spelled out in section 6.5.7 p2 and p3 of the C standard regarding Bitwise Shift Operators:

2 Each of the operands shall have integer type

3 The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

So while the range of an unsigned char should be sufficient to hold any valid value, the right operand will be promoted to int anyway.

over 4 years ago · Santiago Trujillo Relatório

0

C tends to want to do everything as at least int, so it would be very surprising if the RHS of << and >> were to be specified as unsigned short or unsigned char.

It's hard to imagine why a programmer would ever use long (or, god help us, long long) there, but I just tried this code:

int main()
{
    int x = 16;
    long int y = 2;
    int z1 = x << y;
    int z2 = x >> y;
    printf("%d %d\n", z1, z2);

    long long int y2 = 2;
    z1 = x << y2;
    z2 = x >> y2;
    printf("%d %d\n", z1, z2);
}

I compiled it under two compilers, and neither gave any warnings, and both programs printed 64 4. (Not a conclusive test, but suggestive.)

over 4 years ago · Santiago Trujillo Relatório
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