diff --git a/src/plugins/cppeditor/cppquickfix.cpp b/src/plugins/cppeditor/cppquickfix.cpp
index 074543fd0f8738cbcf45bcdba0a72434746713d7..15b0e98027696d521fa475e3d343786a3bc98687 100644
--- a/src/plugins/cppeditor/cppquickfix.cpp
+++ b/src/plugins/cppeditor/cppquickfix.cpp
@@ -97,19 +97,17 @@ protected:
 
 /*
     Rewrite
-    a op b
-
-    As
-    !(a invop b)
+    a op b -> !(a invop b)
+    (a op b) -> !(a invop b)
+    !(a op b) -> (a invob b)
 */
 class UseInverseOp: public QuickFixOperation
 {
 public:
     UseInverseOp()
-        : binary(0)
+        : binary(0), nested(0), negation(0)
     {}
 
-
     virtual QString description() const
     {
         return QLatin1String("Rewrite using ") + replacement; // ### tr?
@@ -151,18 +149,40 @@ public:
         CPlusPlus::Token tok;
         tok.f.kind = invertToken;
         replacement = QLatin1String(tok.spell());
+
+        // check for enclosing nested expression
+        if (index - 1 >= 0)
+            nested = path[index - 1]->asNestedExpression();
+
+        // check for ! before parentheses
+        if (nested && index - 2 >= 0) {
+            negation = path[index - 2]->asUnaryExpression();
+            if (negation && ! tokenAt(negation->unary_op_token).is(T_EXCLAIM))
+                negation = 0;
+        }
+
         return index;
     }
 
     virtual void createChangeSet()
     {
-        insert(startOf(binary), "!(");
-        insert(endOf(binary), ")");
+        if (negation) {
+            // can't remove parentheses since that might break precedence
+            remove(negation->unary_op_token);
+        } else if (nested) {
+            insert(startOf(nested), "!");
+        } else {
+            insert(startOf(binary), "!(");
+            insert(endOf(binary), ")");
+        }
         replace(binary->binary_op_token, replacement);
     }
 
 private:
     BinaryExpressionAST *binary;
+    NestedExpressionAST *nested;
+    UnaryExpressionAST *negation;
+
     QString replacement;
 };