unsigned int a =0xffff;
~a;
printf("%x",a);

gives output ffff

Complementing function works here.

What's use of 0x in 0xffff as unsigned int is of 2 bytes only???

Can anybody suggest???

~a flips the bits in a, but doesn't save the result anywhere. On the next line, a is still equal to 0xffff. 0x marks that the value is written in hexadecimal (simlarly, a number preceded by a 0, such as 024, is written in octal). Lastly, and unsigned int is the same size as an int, which is 4 bytes on a 32-bit CPU and 8-bytes on a 64-bit CPU; perhaps you were thinking of a short int?

Anyways if you run the following code, you should get output of "0xffff0000" (assuming 4-byte int)

#include<stdio.h>

int main()
{
  unsigned int a = 0xffff; // same as a = 0x0000ffff
  a = ~a;
  printf("%x\n", a);
  return 0;
}
Comments
good answer - salem

unsigned int a =0xffff;
~a;
printf("%x",a);

gives output ffff

Complementing function works here.

What's use of 0x in 0xffff as unsigned int is of 2 bytes only???

Can anybody suggest???

Correction: size of unsigned int on normally all machines is 4 bytes that is 32 bits. Only that you specified "ffff" doesnt mean that the variable can take only four f's.

Also complementing function doesnt work since you dont store teh result.

And the 0x in front of the number is to specify that the contents of variable is a hexadecimal number (maybe wrong grammar).
Similarly its a '0' for octal numbers.

Run this code to get a better understanding.

int main (void)
{
    unsigned int n = 0x0000 ;
    printf ("\nThe size of unit is %d", sizeof (unsigned int) ) ;
    n = ~n ;
    printf ("\n%x", n) ;

    return 0;
}

HOpe it helped, bye.

[edit] Heh looks like Mr. Infraction was a bit too fast for me ;) [/edit]

Correction: size of unsigned int on normally all machines is 4 bytes that is 32 bits. Only that you specified "ffff" doesnt mean that the variable can take only four f's.

Also complementing function doesnt work since you dont store teh result.

And the 0x in front of the number is to specify that the contents of variable is a hexadecimal number (maybe wrong grammar).
Similarly its a '0' for octal numbers.

Run this code to get a better understanding.

int main (void)
{
    unsigned int n = 0x0000 ;
    printf ("\nThe size of unit is %d", sizeof (unsigned int) ) ;
    n = ~n ;
    printf ("\n%x", n) ;

    return 0;
}

HOpe it helped, bye.

[edit] Heh looks like Mr. Infraction was a bit too fast for me ;) [/edit]

Thanks . I caught it.

Lastly, and unsigned int is the same size as an int, which is 4 bytes on a 32-bit CPU and 8-bytes on a 64-bit CPU

Maybe true, maybe not. Making size assumptions is not really helpful.

This article has been dead for over six months. Start a new discussion instead.