I wanna change all the 0 to 1 & all the 1 to 0e.g.i have
11100110and I wanna chang it to 00011001
Do I have to use ! Or ~ ?
! vs ~
~
To add:
! is "logical" not. This means it's used for switching the logical values "true" and "false". You might typically use it in an if() statement perhaps such as:
if (!(foo > 7)) { ...
without '!' this would perform the code if foo was greater than 7. With the '!' which exchanges true and false it inverts the sense of the test so it will perform the code when foo is 7 or less. Obviously the way you'd normally write that is:
if (foo <= 7) { ...
but sometimes it's just easier to write an often more complex test then use '!' to say "not that".
'~' on the other hand is bitwise not. This just takes every 1 in the given value and turned it to 0 and every 0 and turns it to 1. So ~0x55 (in 8 bits) is 0xAA and so on.
'!' is an equality operation.
'~' is a manipulation operation.
if (foobar != poopoo) { Do something if foobar is NOT equal to poopoo. }
In the above '!' is used as an inequality operation (NOT EQUAL) between two values. Do something only IF foobar is NOT equal to poopoo.
Whereas:
Foobar = ~poopoo; // Invert poopoo and place the result in foobar.
The second operation above uses the '~' operator to invert the data in poopoo. That is '~'Inverts the data in poopoo (0b0101 0101), to (0b1010 1010), and the result of that operation is placed in foobar.
'!' is an equality operation.
No it isn't. It's a unary operator. You are thinking about "!=" which is something completely different (a relational operator) to "!".
If I say:
x = !3;
I expect it to contain 0 because 3 is a non-zero value so is considered "true" and the opposite of "true" is "false" which is represented by 0.
See also:
http://en.cppreference.com/w/c/l...
Note that '!' has precedence 2 while != has precedence 7. Quite different.
'!' is an equality operation.
No it isn't. It's a unary operator. You are thinking about "!=" which is something completely different (a relational operator) to "!".
If I say:
x = !3;I expect it to contain 0 because 3 is a non-zero value so is considered "true" and the opposite of "true" is "false" which is represented by 0.
See also:
http://en.cppreference.com/w/c/language/operator_precedence
Note that '!' has precedence 2 while != has precedence 7. Quite different.
I figured I'd get someone's attention. It seems I always do...
You know, I thought hard about the proper terminology and, inequality is the closest I could come up with, for !having (NOT) finished my first cup of Coffee of the morning. Are you confused now?
Well, there is no denying... you are absolutely correct! My use of it is also correct, in that '!=" is, in fact, an inequality operation ---> strictly TRUE/FALSE in nature.
The fact is, I don't see the Unary function (in the strictest sense) used much - at least not consciously. Of course, though, I'm not a professional programmer - just a guy who tinkers, really, because I can't afford to pay you to do it for me. Besides, where's the fun in having someone else do it for me???
So, maybe an example of a legitimate use of the '!' (Unary) operator would be good. Can you show us something relevant, Cliff, that would help clear up my misunderstanding, at least? I simply don't recall using the '!' for Unary operations...
My use of it is also correct, in that '!=" is, in fact, an inequality operation
But Cliff's point is that "!=" is a completely separate operator from "!". In your explanation you are saying that the "!" is somehow modifying the "=". It isn't. "!=" is a complete operator used only for comparison, but does not modify anything. A "!" alone modifies the value of a variable in a similar way that "-" or "~" do.
an example of a legitimate use of the '!' (Unary) operator would be good
while (!(UCSR0A & (1<<UDRE0)));
Waits for UDR0E to be set.
volatile uint8_t wdt_tripped; . . . ISR(WDT_vect) { wdt_tripped = 1; } . . . while (!wdt_tripped); // Wait for WDT to expire
Lots more.
Actually, while the proper term for the '!' operation is Unary, the chart you provided in the link clearly states:
! ~ Logical NOT and bitwise NOT
While '!' is clearly Unary (my interpretation being singular or oneness), when used in conjunction with the '=' (equality operation) the combination of the two '!=" become an "Inequality" statement.
At any rate, I have shown two examples using '!' (NOT) and '~' (Bit wise) operators, which IS what the OP was requesting.
Quote:
an example of a legitimate use of the '!' (Unary) operator would be good
while (!(UCSR0A & (1<<UDRE0)));Waits for UDR0E to be set.
volatile uint8_t wdt_tripped; . . . ISR(WDT_vect) { wdt_tripped = 1; } . . . while (!wdt_tripped); // Wait for WDT to expire
Lots more.
Thank for all the answer I understood complete and deeply thnak u all .I appreciate ur respecting.
My use of it is also correct, in that '!=" is, in fact, an inequality operation
But Cliff's point is that "!=" is a completely separate operator from "!". In your explanation you are saying that the "!" is somehow modifying the "=". It isn't. "!=" is a complete operator used only for comparison, but does not modify anything. A "!" alone modifies the value of a variable in a similar way that "-" or "~" do.
Sometimes, the meaning of words get distorted.
Putting a different flavor on it:
While '!' and '=' are two different things and at a higher level, "!=" is something altogether different, at the most basic machine code level, aren't '!', '=' and '!=" are really three separate and distinct Unary instructions ---> NOT, EQUAL and NOT EQUAL?
the proper term for the '!' operation is Unary
Errr - no, it's not.
"Unary" just means "takes one operand": as opposed to a Binary operator, which takes two; or a ternary operator, which takes three; etc...
So the proper term is the Unary (logical) NOT operator.
'!=" become an "Inequality" statement
It's an Operator - not a statement.
'~' (Bit wise) operator
Similarly, "Bit wise" simply means that it acts on the bits individually
So the proper term is the bitwise complement operator.
a ternary operator, which takes three;
The 'C' programming language has only one ternary operator; hence, in the context of the 'C' programming language, we can refer to "the ternary operator" - but there are several unary & binary operators.
'!', '=' and '!=" are really three separate and distinct Unary instructions (sic)
No!
Of those three, only '!' is a unary operator;
'=' and '!=' are both binary operators.
And note that '=' is an assignment operator;
'==' and '!=' are relational operators.
microcarl wrote:
'!', '=' and '!=" are really three separate and distinct Unary instructions (sic)
No!
Of those three, only '!' is a unary operator;
'=' and '!=' are both binary operators.
And note that '=' is an assignment operator;
'==' and '!=' are relational operators.
If it works, use it...
Not knowing how something works can lead to bugs that you don't understand.