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

127
Visualizações
Typecasting Inversion

I really do not understand why I have to typecast the variable b after inversion (unary operator ~). Can anybody explain why this is needed?

unsigned char a = 0xFF;
unsigned char b = 0x00; 

return (a == (~b));                //expected to return 1 but 0

...

return (a == (unsigned char)(~b)); //after typecast returns 1 as expected
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Result of ~b is of promoted type int (Generally result of b with any other unary operator +, - or ~) so you need to typecast the result.

From C11 spec draft section 6.5.3.3 Unary arithmetic operator:

The result of the ~ operator is the bitwise complement of its (promoted) operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set). The integer promotions are performed on the operand, and the result has the promoted type. If the promoted type is an unsigned type, the expression ~E is equivalent to the maximum value representable in that type minus E.

So,

unsigned char b = 0x00;
/* ~b = 0xFFFFFFFF (assuming 4 byte int), (unsigned char)~b = 0xFF */
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