Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-30
3
Medium Priority
?
175 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 84

Accepted Solution

by:
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.
0
 
LVL 4

Assisted Solution

by:Monika Bharti
Monika Bharti earned 501 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 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.

-saige-
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

664 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