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???
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???
Quick summary and a few clarifications that build on the helpful replies from , , , and .
The literal 0xffff is not intrinsically “16‑bit” — the compiler picks the first type from this list in which the value fits: int, unsigned int, long int, unsigned long int, long long int, unsigned long long int. On most modern platforms 0xFFFF will be an int, but on a platform where int is 16 bits the same token becomes unsigned int. That choice affects promotions and conversions when you do bitwise ops or assignments.
A couple of practical rules to avoid surprises:
~ and other bitwise operators are safest on unsigned because unsigned arithmetic has well-defined modulo behavior. U, UL) or, better, fixed-width types from <stdint.h> such as uint32_t. %x expects unsigned int; for fixed-width types prefer the PRIxNN macros from <inttypes.h>.Example (portable pattern):
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main(void) {
uint32_t a = UINT32_C(0xFFFF); /* guaranteed 32-bit literal */
a = ~a; /* store the complemented value */
printf("0x%" PRIX32 "\n", a); /* portable printing */
return 0;
} Checklist: always assign ~a if you want the result, check sizeof when porting (print it with %zu), avoid assuming int is 8 bytes on 64‑bit systems (it often remains 4 bytes), and prefer fixed-width unsigned types for bit fiddling.
Jump to Post— Infarction 503
~aflips 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 …
~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;
} 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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.