Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2015-01-30
Medium Priority
Last Modified: 2015-02-03

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!!!
Question by:Rad1
LVL 84

Accepted Solution

Dave Baldwin earned 498 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.

Assisted Solution

by:Monika Bharti
Monika Bharti earned 501 total points
ID: 40581203
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.
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.
LVL 35

Assisted Solution

it_saige earned 501 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.


Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

773 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