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

132
Visualizações
What is the value of number left shift by -1

What is the result of number when it is left shifted by -1 in C programming using the left shift operator?

e.g.:

23 << -1
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

From the C11 standard 6.5.7p3 (for former versions it is basically the same):

"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."

IOW: undefined behaviour. Prepare for nasal demons.

Briefly: Do not

Caution: While many programmers are aware of that and avoid negative shift counts, it is often ignored that also counts >= the bit-size of the value are also undefined. This makes something like ((unsigned int)1 << 32) - 1 actually undefined if unsigned int has 32 bits or less. For signed values things become more complicated due to the sign (thanks @chux for pointing me at that). This is a common pitfall. For some implementations, different results for constant expressions (compile-time evaluated) and run-time evaluation might occur.

over 4 years ago · Santiago Trujillo Relatório

0

As Olaf said in his answer, left shifting by a negative number is undefined.

In fact, gcc will give a warning if you attempt shift in either direction by a negative number.

over 4 years ago · Santiago Trujillo Relatório

0

Bitwise Left Shift Operator in C:

  1. Bit Pattern of the data can be shifted by specified number of Positions to Left
  2. When Data is Shifted Left , trailing zero’s are filled with zero.
  3. Left shift Operator is Binary Operator [Bi – two]
  4. Binary means , Operator that require two arguments!

Table

Syntax:

**[variable]<<[number of places]**

If Number of Places become negative, it will be undefined

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