Go to Post 1114. You can never count out those sneaky Canadians. - jblay [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 12-01-2004, 19:42
SeanCassidy's Avatar
SeanCassidy SeanCassidy is offline
Antiregistered User
#0263 (Aftershock)
Team Role: Programmer
 
Join Date: Oct 2003
Location: Holtsville, NY
Posts: 37
SeanCassidy is an unknown quantity at this point
Logical And Bitwise AND

I was looking over the default code, and was confused on one part. Here is the code:
Code:
relay1_fwd = p1_sw_trig & rc_dig_in01;  /* FWD only if switch1 is not closed. */
Now knowing regular C, I would say & is bitwise AND. But then I saw the comment above it, "The & used below is the C symbol for AND." And I wasn't sure anymore if it was logical or bitwise AND. Could someone clarify for me?
  #2   Spotlight this post!  
Unread 12-01-2004, 19:47
jeremy562 jeremy562 is offline
Engineer
#0562 (SPARK 562)
 
Join Date: Feb 2002
Location: Fitchburg, MA
Posts: 74
jeremy562 is an unknown quantity at this point
Send a message via AIM to jeremy562
Re: Logical And Bitwise AND

Quote:
Originally Posted by SeanCassidy
I was looking over the default code, and was confused on one part. Here is the code:
Code:
relay1_fwd = p1_sw_trig & rc_dig_in01;  /* FWD only if switch1 is not closed. */
Now knowing regular C, I would say & is bitwise AND. But then I saw the comment above it, "The & used below is the C symbol for AND." And I wasn't sure anymore if it was logical or bitwise AND. Could someone clarify for me?
It's the symbol for bitwise AND. Logical AND in C is always &&.
__________________
SPARK 562: Students Pursuing Applied Robotics Knowledge

2006 BAE Granite State Regional: Finalists with 319 and 176
2006 BAE Granite State Regional: Winners of Motorola Quality Award
2004 BAE Granite State Regional: 7th Seed
2004 Mayhem on the Merrimack Champions! Thanks, 61 and 1289!
  #3   Spotlight this post!  
Unread 12-01-2004, 19:48
SeanCassidy's Avatar
SeanCassidy SeanCassidy is offline
Antiregistered User
#0263 (Aftershock)
Team Role: Programmer
 
Join Date: Oct 2003
Location: Holtsville, NY
Posts: 37
SeanCassidy is an unknown quantity at this point
Re: Logical And Bitwise AND

Thanks. Wasn't sure how much Pic-C changed C.
  #4   Spotlight this post!  
Unread 16-01-2004, 15:10
Atheist Atheist is offline
Registered User
#0555
 
Join Date: Jan 2004
Location: New Jersey
Posts: 22
Atheist is on a distinguished road
Re: Logical And Bitwise AND

Bitwise AND can be used for the same purpose as logical AND. It looks like that is the case here.
  #5   Spotlight this post!  
Unread 16-01-2004, 17:38
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: Logical And Bitwise AND

Quote:
Originally Posted by Atheist
Bitwise AND can be used for the same purpose as logical AND. It looks like that is the case here.
Absolutely NOT TRUE. Again as I've said in previous posts, if you do not know what you're talking about, do not pretend that you do and make untrue assertions. It's best to keep quiet and not confuse people who may believe what you post.

The two are only interchangable here because both expressions of the operand are bits. If they weren't both bits you could get different results with the two operators.

Bitwise AND looks at each bit of the two numbers and does a logical AND on the corresponding bits. Bits in the result are 1 if the corresponding bits in the two operands are both 1, else the bits are 0.

Logical AND looks at the whole expression on each side. If both expressions are nonzero then the AND expression evaluates to TRUE (the value 1 in C).

This snippet of code:
Code:
int a = 0x03;
int b = 0x06;
printf("%d %d\n", (a && b), (a & b));
prints:
Code:
1 2
Why? (a && b) evaluates to TRUE because both numbers are nonzero, so we see a 1. (a & b) in binary is (0011 & 1010) and it evaluates to 0010, or 2.
  #6   Spotlight this post!  
Unread 16-01-2004, 18:06
Skabana159's Avatar
Skabana159 Skabana159 is offline
Robotics and Field Hockey
AKA: Jesse C. Owens
#0159 (Alpine Robotics)
Team Role: Mentor
 
Join Date: Mar 2002
Rookie Year: 2000
Location: Ft. Collins, CO
Posts: 92
Skabana159 is on a distinguished road
Send a message via AIM to Skabana159
Re: Logical And Bitwise AND

Quote:
Originally Posted by Atheist
Bitwise AND can be used for the same purpose as logical AND. It looks like that is the case here.
Quote:
Originally Posted by Mike Soukup
Absolutely NOT TRUE.
Easy now! He said that it CAN BE USED for the same purpose, not that they are the same thing.
What he means is that a lot of time, either & bitwise or && logical can be used, because both operands are conditionals, thus they are both bits.
__________________
"What most people do not understand is that the Buddha, the Godhead, resides just as comfortably in gears and circuits as in hills and trees. To believe otherwise is to dilute the Godhead."
-Robert Pirsig, Zen and the Art of Motorcycle Maintenance
  #7   Spotlight this post!  
Unread 16-01-2004, 18:18
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Logical And Bitwise AND

Even if he did, it was still ambiguous enough to require clarification.
  #8   Spotlight this post!  
Unread 16-01-2004, 18:50
Atheist Atheist is offline
Registered User
#0555
 
Join Date: Jan 2004
Location: New Jersey
Posts: 22
Atheist is on a distinguished road
Re: Logical And Bitwise AND

Sorry, didn't mean to cause confusion. All I was saying is that the two operands can be used to the same end. Obviously they are not interchangeable within the same statement and their technical function is very different.
  #9   Spotlight this post!  
Unread 16-01-2004, 19:15
Raven_Writer's Avatar
Raven_Writer Raven_Writer is offline
2004 Detroit & Pittsburgh Winners
AKA: Eric Hansen
FRC #0005 (RoboCards)
Team Role: Mentor
 
Join Date: Jan 2003
Rookie Year: 2002
Location: Melvindale
Posts: 1,549
Raven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really niceRaven_Writer is just really nice
Send a message via ICQ to Raven_Writer Send a message via AIM to Raven_Writer Send a message via MSN to Raven_Writer Send a message via Yahoo to Raven_Writer
Re: Logical And Bitwise AND

I always thought that & pointed to the address of a variable, example:

Code:
char *ptr;
 
printf("%s", &ptr);
I've never used pointers much/at all before, but I this is what I remember reading.

(Sorry if I'm wrong about this)
__________________
AIM: wisprmylastbreth
EMail: nightskywriter@gmail.com
Y!: synsoflife

"ai yoru ga" -- "Love the nights"
  #10   Spotlight this post!  
Unread 16-01-2004, 19:59
Atheist Atheist is offline
Registered User
#0555
 
Join Date: Jan 2004
Location: New Jersey
Posts: 22
Atheist is on a distinguished road
Re: Logical And Bitwise AND

& has a number of functions, including those stated previously in this thread as well as what you just mentioned.
  #11   Spotlight this post!  
Unread 17-01-2004, 01:03
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Logical And Bitwise AND

what you're thinking of is & prefixed to something, which does give the address - & standing alone is the bitwise AND.
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
  #12   Spotlight this post!  
Unread 17-01-2004, 15:58
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: Logical And Bitwise AND

Quote:
Originally Posted by Skabana159
Easy now! He said that it CAN BE USED for the same purpose, not that they are the same thing.
What he means is that a lot of time, either & bitwise or && logical can be used, because both operands are conditionals, thus they are both bits.
Not true. In almost every instance & and && can not be used for the same purpose. The only time they are guaranteed to return the same result is if the two operands are bits.

Quote:
Originally Posted by Atheist
All I was saying is that the two operands can be used to the same end.
Absolutely not true again. They have different meanings. They translate into different machine instructions. They give the same result only for very limited scope.

What everyone is saying is analogous to saying that since 2*2 is equal to 4, and 2 + 2 is also equal to 4, the two operands can both be used to the same end. Just because an operation equates to the same number once does not mean they are the same.
  #13   Spotlight this post!  
Unread 17-01-2004, 16:25
Atheist Atheist is offline
Registered User
#0555
 
Join Date: Jan 2004
Location: New Jersey
Posts: 22
Atheist is on a distinguished road
Re: Logical And Bitwise AND

Quote:
Originally Posted by Mike Soukup
Not true. In almost every instance & and && can not be used for the same purpose. The only time they are guaranteed to return the same result is if the two operands are bits.


Absolutely not true again. They have different meanings. They translate into different machine instructions. They give the same result only for very limited scope.

What everyone is saying is analogous to saying that since 2*2 is equal to 4, and 2 + 2 is also equal to 4, the two operands can both be used to the same end. Just because an operation equates to the same number once does not mean they are the same.
And what you are saying is that what we are saying is that they are the same. This is absolutely not true. No one in this thread stated that they are the same.

Your example is not quite what I had in mind, however, imagine it in base 2.
  #14   Spotlight this post!  
Unread 17-01-2004, 16:32
djcapelis's Avatar
djcapelis djcapelis is offline
Fried Manic Custard
None #0675 (Geeks with Power Tools)
Team Role: Programmer
 
Join Date: May 2003
Rookie Year: 2001
Location: Rohnert Park, CA
Posts: 129
djcapelis will become famous soon enoughdjcapelis will become famous soon enough
Send a message via ICQ to djcapelis Send a message via AIM to djcapelis Send a message via Yahoo to djcapelis
Re: Logical And Bitwise AND

I think the matter has been clarified for even the casual reader now and all your purposes have been fullfilled.

The argument can and should probably die away now.

The gist: C is a complex language and one line of code and operand can be used for many different things in many different circumstances. Know the differences between all of them, if you can express something in two different ways in C there's almost always a reason, know it.
__________________
"I have more friends than enemies, I'm working to resolve the issue."
  #15   Spotlight this post!  
Unread 18-01-2004, 00:22
SeanCassidy's Avatar
SeanCassidy SeanCassidy is offline
Antiregistered User
#0263 (Aftershock)
Team Role: Programmer
 
Join Date: Oct 2003
Location: Holtsville, NY
Posts: 37
SeanCassidy is an unknown quantity at this point
Re: Logical And Bitwise AND

And if I remember correctly, & can be used to make a reference to a variable or other thing, but doesn't require the pointer syntax. (NOTE: This is out of my C++ Book and I do not have any idea if it does go for C, it might)

Code:
int ival = 1024;
int &refVal = ival;        // ok: refVal is a reference to ival
int &refVal2;               // no: refVal2 needs to be initalized.
Not wanting to bring up the flame war on this "heated" discussion (hehe), but just so you know ampersand (&) is the address-of operator, bitwise AND, logical AND (in pairs, &&), and reference operator. So watch those &'s!
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
PBASIC language syntax WizardOfAz Programming 14 30-04-2003 10:23
Yes This IS a Beginner's Question cammie825 Programming 8 11-02-2003 22:16


All times are GMT -5. The time now is 19:52.

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