Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda