what is the meaning of this "~"symbol in c++?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rajesh sangem
    New Member
    • Aug 2013
    • 1

    what is the meaning of this "~"symbol in c++?

    hello.... guys

    what is the meaning of this "~"symbol in c++?
    please help me some one............ ..
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It is the bitwise "Not". So ~1 means "not 1", which would be 0 since that's the only other bit value possible.

    Comment

    • kudos
      Recognized Expert New Member
      • Jul 2006
      • 127

      #3
      It could also be a destructor of a class.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        That's true. But if your class is named MyStuff a constructor would be named MyStuff() and the destructor would be
        ~MyStuff(). That is, ~MyStuff() is not a constructor.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          In C (and probably C++), ~ is the bit-wise negation operator and ! is the logical negation operator. These operators are sometimes confused.

          These are both unary operators; that is, they each have only a single operand.

          The operand to the bit-wise negation operator can be any integral type. The result has the same type as the converted operand. First, the usual unary conversions are performed on the operand, then each bit in the converted operand is flipped: each 1 becomes 0; each 0 becomes 1.

          The operand to the logical negation operator can be any integral type, a pointer type, or any floating-point type. The result is of type int. First, the usual unary conversions are performed on the operand. The result is 1 if the converted operand is 0 (integral type), null (pointer type), or 0.0 (floating-point type); otherwise the result is 0. That is, true becomes false, and false becomes true.

          Examples (for an implementation where int has 16 bits and two's-complement is used to encode negative values):
          Code:
          unsigned char a = 5;
          short b = 0;
          int c = -1;
          int d = 1;
          int e;
          
          e = !a;    // result = 0
          e = !b;    // result = 1
          e = !c;    // result = 0
          e = !d;    // result = 0
          
          e = ~a;    // result = 0xFFFA (which encodes -6)
          e = ~b;    // result = 0xFFFF (which encodes -1)
          e = ~c;    // result = 0x0000 (0)
          e = ~d;    // result = 0xFFFE (which encodes -2)
          Notice that @weaknessforcat s and I anticipate different results for ~1. Sometimes the best investigation comes from writing a sample program to see what happens.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Be careful about ~1. the ~ operator is a bitwise operator and a bit value is either 0 or 1. Therefore, ~1 is 0.

            However, an int is a variable and value of 1 is something like 00000001. The ~ operator results in a value 11111110. The compiler will see the leftmost 1 as a sign bit. A 1 means a negative number and that means 2-s complement encoding making ~1 appear as -2.

            Comment

            Working...