Solved

What are the meanings and differences of Binary and Unary operators (OR / ||, && / &, XOR)

Posted on 2015-01-30
3
161 Views
Last Modified: 2015-02-03
Hello,

Can anyone explain the meanings and differences of Binary and Unary operators?
When and where to use each?  
Note: Not asking for lots of details just good explanation that will clarify the meaning and differences.

OR / ||
&& / &
XOR etc......

Thank you!!!
0
Comment
Question by:Rad1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 166 total points
ID: 40581185
This page http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B is a pretty good list of the operators in C++ and most of them are common to C-derived languages including C#, PHP, and Perl.  Note that almost all are 'binary' operators because they involve 2 operands.  This page http://en.wikipedia.org/wiki/Unary_operation shows unary operators.

Logical operators are usually used to compare the results of two comparisons.

if((a == 23) && (b == 96)) then do something...

Bitwise operators actually operate on the operands themselves.

d = e & f  where e = 0x11 and f = 0x10 would result in d being 0x10.

d = e | f  where e = 0x11 and f = 0x10 would result in d being 0x11.
0
 
LVL 4

Assisted Solution

by:Monika Bharti
Monika Bharti earned 167 total points
ID: 40581203
Hi,
The main difference between unary and binary.
Unary operator operates on single operand while binary operator operates in two operands.

&& / &
This type of search will result in entries that must contain both words. You may use “AND” to narrow a search.
1.jpg
OR / ||
This type of search will result in entries that contain either of the words. Use “OR” to broaden a search. Use this especially with synonyms, or terms that have similar meanings.
2.jpg
0
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 167 total points
ID: 40581530
First lets clear up the intent of the question:

Did you want to know the difference between Unary [+, -, ++, --, *, &, !, ~] and Binary [+, =, -, *, /] operators (just to name a few).

Or Logical [&&, ||, &, |] and Bitwise [&, |, ^, ~] operators (just to name a few).

Unary and Binary is easy, it depends on the operation.  If the operation has 1 operand then the operator is considered to unary (as it operates on 1 operand).  Example: ++i and --i

The interesting thing, however, about ++ and -- is that they actually perform a binary operation (i = i + 1 and i = i - 1, respectively) but because they perform this action on a single operand they are considered to be unary.

As for Logical and Bitwise operators.  Logical operators are used to produce comparison based (or boolean) results.  Example:
(true & false) = false
(true && false) = false
(true | false) = true
(true || false) = true

Open in new window

They both may look the same and you may be asking yourself, well why should I sacrifice an additional key press?  Because of short-circuit logic, both && and || are short circuit operators.

Short circuit logic dictates that if the answer to the questions, (true or false) = true AND (false and true) = false, can be answered by looking at the left hand side of the logical equation, then I do not need to look at the right hand side of the equation.  In the cases of & and |, both sides of the logical equation are resolved to come up with the answer true or false.

-saige-
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Introduction to Processes
Starting up a Project

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question