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

Posted on 2015-01-30
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 83

Accepted Solution

Dave Baldwin earned 166 total points
ID: 40581185
This page 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 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 167 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 33

Assisted Solution

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.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 105
Beginner to Unreal Engine 4 5 94
Windows ICD FFU Issue 4 61
ejb message driven bean mdb creation steps 2 23
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

839 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