|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
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; Thank you very much, |
|
#2
|
|||||
|
|||||
|
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; Last edited by Greg Ross : 02-05-2005 at 20:56. |
|
#3
|
|||||
|
|||||
|
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, |
|
#4
|
|||||
|
|||||
|
Re: Functions of '?', ':', and '&' in the C programming language
Quote:
Assuming Tx_Buffer = 0x41, the comparison would look like: Code:
0000 0001 & 0100 0001 ----------------- 0000 0001 |
|
#5
|
|||||
|
|||||
|
Re: Functions of '?', ':', and '&' in the C programming language
Quote:
Thanks again, |
|
#6
|
||||||
|
||||||
|
Re: Functions of '?', ':', and '&' in the C programming language
Quote:
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... |
|
#7
|
|||||
|
|||||
|
Re: Functions of '?', ':', and '&' in the C programming language
Quote:
Code:
if (Tx_Buffer & 0x01 == 1) {
TX_OUTPUT_PIN = SERIAL_1;
} else {
TX_OUTPUT_PIN = SERIAL_0;
}
Code:
if (Tx_Buffer & 0x01 == 1)
{
TX_OUTPUT_PIN = SERIAL_1;
}
else
{
TX_OUTPUT_PIN = SERIAL_0;
}
![]() |
|
#8
|
|||
|
|||
|
Re: Functions of '?', ':', and '&' in the C programming language
Quote:
|
|
#9
|
||||||
|
||||||
|
Re: Functions of '?', ':', and '&' in the C programming language
Quote:
http://www.usfirst.org/robotics/2005...sentations.htm |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|