11
int m=32
printf("%x" , ~m);

Output of this statement is ffdf and without ~ output is 20. What is the significance of %x and ~?

Brite Roy
  • 425
  • 3
  • 9
  • 28
  • 6
    You might want to go through some of your old questions and accept the best answers, otherwise people may not want to provide as helpful answers. – brc Jan 17 '12 at 19:34

6 Answers6

25

The ~ operator is bitwise negation. It will print bitwise negation of m's value. %x means that printf will output its value in hexadecimal format.

So, value 0xffdf is the negation of value 0x20 (32).

Value 32 (int bits would be):

0000 0000 0010 0000

Its bitwise negation will be:

1111 1111 1101 1111

Which makes sense since:

1111 1111 = 0xff

And:

1101 1111 = 0xdf
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
7

The %x is the printf format that indicates that the int value should be displayed in hexadecimal.

The ~ is bitwise NOT, which flips all the bits in the integer.

The statement:

printf("%x", m);

will display the output 20 as 0x20 = decimal 32.

The statement:

printf("%x", ~m);

will display the output ffdf as 0xffdf is the bitwise inverse of 0x20.

It may make more sense to visualize the bitwise negation in binary:

Base 10:         32                  65503
Base 16:        0x20                0xFFDF
Base 2:    0000000000100000    1111111111011111
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
2

The ~ symbol represents the bitwise NOT, or complement operator; a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Binary digits that are 0 become 1, and those that are 1 become 0.

32 is 00100000 in binary, and ~32 is 11011111 in binary (or 223 in decimal).

The %x option in the printf function will display a unsigned hexadecimal format (using lowercase letters).

So,

printf("%x", m); // displays the hexadecimal value of 32 (00100000), "20"

printf("%x", ~m); // displays the hexadecimal value of ~32 (11101111), "ffdf"


[Sources]
http://en.wikipedia.org/wiki/Bitwise_operation#NOT
http://en.wikipedia.org/wiki/Hexadecimal
http://en.wikipedia.org/wiki/Printf_format_string

Omar
  • 1,493
  • 12
  • 14
1

And %x means you print the value of x in hexadecimale.

delannoyk
  • 1,216
  • 1
  • 12
  • 22
1

~32 = -33 use unsigned int to get results

Fedor Igumnov
  • 201
  • 3
  • 14
1

It means that x should have probably have been declared unsigned rather than int.

The "%x" printf format requires an unsigned int argument, and prints its value in hexadecimal (base 16). You can safely use it with an int argument if the value is within the range representable either as an int or as an unsigned int (i.e., 0 .. INT_MAX). Using "%x" with a negative int value, as this code fragment does, strictly speaking has undefined behavior, though the actual behavior is likely to be reasonably consistent.

The C standard says:

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).

Note that it's defined in terms of the representation of the operand, not its value.

The output you describe indicates that you're using a system with two's-complement signed integers where int is only 16 bits, which is unusual these days. (Are you using an ancient Turbo C compiler or something similar?) On my system, this program:

#include <stdio.h>
int main(void) {
    int m = 32;
    printf("%x\n" , ~m);
    return 0;
}

produces this output:

ffffffdf

(Note that I've added the required #include <stdio.h> and a semicolon on the declaration of m.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631