C operators

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

What is the difference in the following operators in C? what values will they each output in avr?

 

PORTA = !0xff;

PORTA = ~0xff;

PORTA = !PORTA;

PORTA = ~PORTA;

PORTA^=0xff;

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

Put them in your chosen IDE and simulate them.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

What does your 'C' textbook tell you?

 

Here are some links for 'C' learning & reference materials - including a free online textbook: http://blog.antronics.co.uk/2011...

 

Here's a Tutorial on bit manipulation in 'C': https://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101

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

vcoder wrote:

What is the difference in the following operators in C? what values will they each output in avr?

 

PORTA = !0xff;

PORTA = ~0xff;

PORTA = !PORTA;

PORTA = ~PORTA;

PORTA^=0xff;

 

Your main doubt seems to be between bitwise and logical negation. It's simple. Logical negation of any nonzero value is zero, while bitwise negation inverts each bit of the binary representation of the value individually.

Last Edited: Thu. Sep 6, 2018 - 07:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
Put them in your chosen IDE and simulate them.

As they are entirely standard 'C' operators, it doesn't even have to be an AVR IDE, or any microcontroller IDE:  you could do this in any desktop IDE - MS VisualStudio; Borland (or whatever they're called today); whatever ...

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: 2

#4 has most of what you need to know but just to point out that ! is "logical" and ~ is "bitwise".

One way to remember it is that ! turns truth upside down while ~ turns the bits upside down.

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

clawson wrote:
One way to remember it is that ! turns truth upside down while ~ turns the bits upside down.

I like that - will have to remember it!

 

yes

 

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

And I guess the pedantic answer to the question, in several of the cases, is....'it depends on what value PORTA holds before the operation is executed.'

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

Sounds like a school assignment, I would suggest you read your text book, or at least the C references suggested above.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...