Go to Post Isn't that the connector used on the F.L.U.X. Capacitor? - Ed Sparks [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 02-05-2005, 20:38
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
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,
__________________
  #2   Spotlight this post!  
Unread 02-05-2005, 20:49
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
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;
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me

Last edited by Greg Ross : 02-05-2005 at 20:56.
  #3   Spotlight this post!  
Unread 02-05-2005, 21:42
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
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   Spotlight this post!  
Unread 02-05-2005, 22:16
Unsung FIRST Hero
Matt Leese Matt Leese is offline
Been-In-FIRST-Too-Long
FRC #1438 (The Aztechs)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1998
Location: Long Beach, CA
Posts: 937
Matt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond repute
Send a message via AIM to Matt Leese
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
  #5   Spotlight this post!  
Unread 02-05-2005, 22:46
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
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,
__________________
  #6   Spotlight this post!  
Unread 03-05-2005, 09:37
Chris Hibner's Avatar Unsung FIRST Hero
Chris Hibner Chris Hibner is offline
Eschewing Obfuscation Since 1990
AKA: Lars Kamen's Roadie
FRC #0051 (Wings of Fire)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1997
Location: Canton, MI
Posts: 1,488
Chris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond repute
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...
__________________
-
An ounce of perception is worth a pound of obscure.
  #7   Spotlight this post!  
Unread 03-05-2005, 13:43
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
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!
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
  #8   Spotlight this post!  
Unread 04-05-2005, 21:37
Ben Margolis Ben Margolis is offline
Programmer Boy
no team
 
Join Date: Mar 2005
Rookie Year: 2005
Location: San Bruno, CA
Posts: 9
Ben Margolis is a glorious beacon of lightBen Margolis is a glorious beacon of lightBen Margolis is a glorious beacon of lightBen Margolis is a glorious beacon of lightBen Margolis is a glorious beacon of lightBen Margolis is a glorious beacon of light
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.
  #9   Spotlight this post!  
Unread 05-05-2005, 09:25
Chris Hibner's Avatar Unsung FIRST Hero
Chris Hibner Chris Hibner is offline
Eschewing Obfuscation Since 1990
AKA: Lars Kamen's Roadie
FRC #0051 (Wings of Fire)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1997
Location: Canton, MI
Posts: 1,488
Chris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond repute
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
__________________
-
An ounce of perception is worth a pound of obscure.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 00:17.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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