Solved

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

Posted on 2015-01-30
3
130 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
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 166 total points
Comment Utility
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
Comment Utility
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 32

Assisted Solution

by:it_saige
it_saige earned 167 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now