! vs ~

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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 ~ ?

This topic has a solution.
Last Edited: Wed. Aug 9, 2017 - 04:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

~

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

'!' 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.

 

 

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Fri. Jan 16, 2015 - 05:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

'!' 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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

'!' 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? laugh

 

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

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Fri. Jan 16, 2015 - 05:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

 

Regards,
Steve A.

The Board helps those that help themselves.

Last Edited: Fri. Jan 16, 2015 - 06:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Fri. Jan 16, 2015 - 06:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Sat. Jan 17, 2015 - 03:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:

 

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 you Joey...

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank for all the answer I understood complete and deeply thnak u all .I appreciate ur respecting.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:

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.

 

Thanks, Koshchi.

 

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?

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Sat. Jan 17, 2015 - 03:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

microcarl wrote:
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.

 

Quote:
 '!=" become an "Inequality" statement

It's an Operator - not a statement.

 

Quote:
 '~' (Bit wise) operator

Similarly, "Bit wise" simply means that it acts on the bits individually

 

So the proper term is the bitwise complement operator.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
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.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

 

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.

Syntax semantics...  Or, is that Syntax antics?  If it works, use it...

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If it works, use it...

Not knowing how something works can lead to bugs that you don't understand. 

Regards,
Steve A.

The Board helps those that help themselves.

Last Edited: Sun. Jan 18, 2015 - 07:30 PM