954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Operation on bits

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???

himanjim
Junior Poster in Training
67 posts since Jul 2006
Reputation Points: 14
Solved Threads: 1
 

~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;
}
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

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]

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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.

himanjim
Junior Poster in Training
67 posts since Jul 2006
Reputation Points: 14
Solved Threads: 1
 
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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You