Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Functions of '?', ':', and '&' in the C programming language (http://www.chiefdelphi.com/forums/showthread.php?t=37867)

DanDon 02-05-2005 20:38

Functions of '?', ':', and '&' in the C programming language
 
What are the functions of the ? and the : in the following C statement?

Code:


TX_OUTPUT_PIN = Tx_Buffer & 0x01 ? SERIAL_1 : SERIAL_0;

Another question, can someone please clearly explain the purpose (And usage) of the bitwise AND operator?

Thank you very much,

Greg Ross 02-05-2005 20:49

Re: Functions of '?', ':', and '&' in the C programming language
 
The ?: is called the ternary operator because it takes three operands. It's shorthand for "If the first operand is true, return the value of the second operand, otherwise, return the value of the third."

The & is a bitwise AND. The value returned has a 1 in each bit that is set in both operands. All other bits are zero.

In this example, the low order bit of Tx_Buffer is tested, and if it is set, TX_OUTPUT_PIN is assigned the value SERIAL_1, otherwise SERIAL_0.

BTW, I generally steer away from using the ternary operator. The following is much clearer:
Code:

if (Tx_Buffer & 0x01 == 1)
  TX_OUTPUT_PIN = SERIAL_1;
else
  TX_OUTPUT_PIN = SERIAL_0;


DanDon 02-05-2005 21:42

Re: Functions of '?', ':', and '&' in the C programming language
 
Please pardon my ignorance, but what does the 0x01 do when the bitwise AND is used on it and Tx_Buffer?

Thank you so much for your reply,

Matt Leese 02-05-2005 22:16

Re: Functions of '?', ':', and '&' in the C programming language
 
Quote:

Originally Posted by dhoizner
Please pardon my ignorance, but what does the 0x01 do when the bitwise AND is used on it and Tx_Buffer?

Thank you so much for your reply,

The bitwise operators work at the bit level. It will do a bitwise AND on each and every bit. So, if the low-bit of Tx_Buffer is 1, the output will be 1. Otherwise it will be 0.

Assuming Tx_Buffer = 0x41, the comparison would look like:

Code:

   
  0000 0001
& 0100 0001
-----------------
  0000 0001

Matt

DanDon 02-05-2005 22:46

Re: Functions of '?', ':', and '&' in the C programming language
 
Quote:

Originally Posted by Matt Leese
The bitwise operators work at the bit level. It will do a bitwise AND on each and every bit. So, if the low-bit of Tx_Buffer is 1, the output will be 1. Otherwise it will be 0.

Assuming Tx_Buffer = 0x41, the comparison would look like:

Code:


0000 0001
& 0100 0001
-----------------
0000 0001

Matt

Thanks for the reply Matt, that really clarified my understanding of the bitwise AND operator.

Thanks again,

Chris Hibner 03-05-2005 09:37

Re: Functions of '?', ':', and '&' in the C programming language
 
Quote:

Originally Posted by GW (Greg) Ross
The ?: is called the ternary operator because it takes three operands. It's shorthand for "If the first operand is true, return the value of the second operand, otherwise, return the value of the third."

The & is a bitwise AND. The value returned has a 1 in each bit that is set in both operands. All other bits are zero.

In this example, the low order bit of Tx_Buffer is tested, and if it is set, TX_OUTPUT_PIN is assigned the value SERIAL_1, otherwise SERIAL_0.

BTW, I generally steer away from using the ternary operator. The following is much clearer:
Code:

if (Tx_Buffer & 0x01 == 1)
  TX_OUTPUT_PIN = SERIAL_1;
else
  TX_OUTPUT_PIN = SERIAL_0;


As much as I hate "yeah, me too" posts, I'm going to do it here...

I strongly recommend staying away from the ternary operator. I also strongly recommend staying away from any other practice that makes your code less clear than it has to be. Good clear code trumps keystroke efficiency in all cases - don't forget that.

During the FRC Championship conference, myself and a colleagure presented an "Advanced C Programming" lecture. The main focus of the lecture was to show how C programming is done in the real-time embedded industry. We tried to impress as much as possible on "good coding practices". While it wasn't in the presentation, we made mention of staying away from the ternary operator (as well as recursion, compound statements, leaving out the {} from code blocks, etc.).

The following web page: http://www.andromeda.com/people/ddyer/topten.html is a great list of C "gotcha's". 90% of them are avoided by using the good practices we recommend. As a side note, the good coding practices that we recommend are basically part of a coding standard that is typical in industry.

Okay, sorry for getting this thread off topic...

Greg Ross 03-05-2005 13:43

Re: Functions of '?', ':', and '&' in the C programming language
 
Quote:

Originally Posted by Chris Hibner
...(as well as recursion, compound statements, leaving out the {} from code blocks, etc.).

Yeah. I know I violated this one. I should have written
Code:

if (Tx_Buffer & 0x01 == 1) {
  TX_OUTPUT_PIN = SERIAL_1;
} else {
  TX_OUTPUT_PIN = SERIAL_0;
}

(Or is it
Code:

if (Tx_Buffer & 0x01 == 1)
  {
    TX_OUTPUT_PIN = SERIAL_1;
  }
else
  {
    TX_OUTPUT_PIN = SERIAL_0;
  }

?) There are so many opinions about where to place the braces, I decided to leave them out... just this once! I swear! :o

Ben Margolis 04-05-2005 21:37

Re: Functions of '?', ':', and '&' in the C programming language
 
Quote:

Originally Posted by Chris Hibner
During the FRC Championship conference, myself and a colleagure presented an "Advanced C Programming" lecture.

I just want to say that I like the website and I'm really sorry I missed the lecture. Is there anywhere where an overview of it can be found? I'm all for spreading the C knowledge.

Chris Hibner 05-05-2005 09:25

Re: Functions of '?', ':', and '&' in the C programming language
 
Quote:

Originally Posted by Ben Margolis
I just want to say that I like the website and I'm really sorry I missed the lecture. Is there anywhere where an overview of it can be found? I'm all for spreading the C knowledge.

The conference material can be found here:

http://www.usfirst.org/robotics/2005...sentations.htm


All times are GMT -5. The time now is 04:41.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi