Binary Operators in Programming
Last Updated :
20 Mar, 2024
Binary Operators are essential tools in programming that perform operations on pairs of data, enabling tasks like calculations, comparisons, logical operations, and bitwise manipulations. They are fundamental for processing and manipulating data efficiently in computer programs.
What are Binary Operators?
Binary Operators are operators in programming that perform operations on two operands. These operands can be variables, constants, or expressions. Binary operators are called binary because they operate on two operands.
Basics of Binary Operators:
Binary operators, as the name suggests, operate on two operands and perform various computations or comparisons. These operators are integral to arithmetic, bitwise operations, and relational evaluations within programming languages. Here, we'll discuss common binary operators and their applications.
Arithmetic Binary Operators:
Arithmetic binary operators handle basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. The syntax for these operators is consistent across many languages:
operand1 + operand2 // Addition
operand1 - operand2 // Subtraction
operand1 * operand2 // Multiplication
operand1 / operand2 // Division
operand1 % operand2 // Modulus
Bitwise operators manipulate individual bits of binary numbers. These operators are often used in low-level programming for tasks like system-level operations, network programming, or optimization. The commonly used bitwise operators include AND, OR, XOR, left shift, and right shift:
operand1 & operand2 // Bitwise AND
operand1 | operand2 // Bitwise OR
operand1 ^ operand2 // Bitwise XOR
operand1 << n // Left shift by n bits
operand1 >> n // Right shift by n bits
Logical Binary Operators:
Logical operators are used to combine two or more conditions and evaluate them to produce a Boolean result:
operand1 && operand2 // Logical AND
operand1 || operand2 // Logical OR
!operand // Logical NOT
Relational Binary Operators:
Relational operators compare two values and return a Boolean result, indicating whether the relationship is true or false:
operand1 == operand2 // Equal to
operand1 != operand2 // Not equal to
operand1 > operand2 // Greater than
operand1 < operand2 // Less than
operand1 >= operand2 // Greater than or equal to
operand1 <= operand2 // Less than or equal to
Binary Operator in C:
Here are the implementation of Binary Operator in C language:
C
#include <stdio.h>
int main()
{
int a = 10;
int b = 5;
// Binary addition (+)
printf("a + b = %d\n", a + b);
// Binary subtraction (-)
printf("a - b = %d\n", a - b);
// Binary multiplication (*)
printf("a * b = %d\n", a * b);
// Binary division (/)
printf("a / b = %d\n", a / b);
// Binary modulo (%)
printf("a % b = %d\n", a % b);
// Binary bitwise AND (&)
printf("a & b = %d\n", a & b);
// Binary bitwise OR (|)
printf("a | b = %d\n", a | b);
// Binary bitwise XOR (^)
printf("a ^ b = %d\n", a ^ b);
// Binary left shift (<<)
printf("a << 1 = %d\n", a << 1);
// Binary right shift (>>)
printf("a >> 1 = %d\n", a >> 1);
// Logical AND (&&)
printf("(a > 0) && (b > 0) = %d\n", (a > 0) && (b > 0));
// Logical OR (||)
printf("(a > 0) || (b > 0) = %d\n", (a > 0) || (b > 0));
// Equal to (==)
printf("a == b = %d\n", a == b);
// Not equal to (!=)
printf("a != b = %d\n", a != b);
// Greater than (>)
printf("a > b = %d\n", a > b);
// Less than (<)
printf("a < b = %d\n", a < b);
// Greater than or equal to (>=)
printf("a >= b = %d\n", a >= b);
// Less than or equal to (<=)
printf("a <= b = %d\n", a <= b);
return 0;
}
Outputa + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
a & b = 0
a | b = 15
a ^ b = 15
a << 1 = 20
a >> 1 = 5
(a > 0) && (b > 0) = 1
(a > 0) || (b > 0) = 1
a == b = 0
a != b = 1
a > b = 1
a < b = 0
a >= ...
Binary Operator in C++:
Here are the implementation of Binary Operator in C++ language:
C++
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 5;
// Binary addition (+)
cout << "a + b = " << a + b << endl;
// Binary subtraction (-)
cout << "a - b = " << a - b << endl;
// Binary multiplication (*)
cout << "a * b = " << a * b << endl;
// Binary division (/)
cout << "a / b = " << a / b << endl;
// Binary modulo (%)
cout << "a % b = " << a % b << endl;
// Binary bitwise AND (&)
cout << "a & b = " << (a & b) << endl;
// Binary bitwise OR (|)
cout << "a | b = " << (a | b) << endl;
// Binary bitwise XOR (^)
cout << "a ^ b = " << (a ^ b) << endl;
// Binary left shift (<<)
cout << "a << 1 = " << (a << 1) << endl;
// Binary right shift (>>)
cout << "a >> 1 = " << (a >> 1) << endl;
// Logical AND (&&)
cout << "(a > 0) && (b > 0) = " << ((a > 0) && (b > 0))
<< endl;
// Logical OR (||)
cout << "(a > 0) || (b > 0) = " << ((a > 0) || (b > 0))
<< endl;
// Equal to (==)
cout << "a == b = " << (a == b) << endl;
// Not equal to (!=)
cout << "a != b = " << (a != b) << endl;
// Greater than (>)
cout << "a > b = " << (a > b) << endl;
// Less than (<)
cout << "a < b = " << (a < b) << endl;
// Greater than or equal to (>=)
cout << "a >= b = " << (a >= b) << endl;
// Less than or equal to (<=)
cout << "a <= b = " << (a <= b) << endl;
return 0;
}
Outputa + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
a & b = 0
a | b = 15
a ^ b = 15
a << 1 = 20
a >> 1 = 5
(a > 0) && (b > 0) = 1
(a > 0) || (b > 0) = 1
a == b = 0
a != b = 1
a > b = 1
a < b = 0
a >= ...
Binary Operator in Java:
Here are the implementation of Binary Operator in Java language:
Java
public class Main {
public static void main(String[] args)
{
int a = 10;
int b = 5;
// Binary addition (+)
System.out.println("a + b = " + (a + b));
// Binary subtraction (-)
System.out.println("a - b = " + (a - b));
// Binary multiplication (*)
System.out.println("a * b = " + (a * b));
// Binary division (/)
System.out.println("a / b = " + (a / b));
// Binary modulo (%)
System.out.println("a % b = " + (a % b));
// Binary bitwise AND (&)
System.out.println("a & b = " + (a & b));
// Binary bitwise OR (|)
System.out.println("a | b = " + (a | b));
// Binary bitwise XOR (^)
System.out.println("a ^ b = " + (a ^ b));
// Binary left shift (<<)
System.out.println("a << 1 = " + (a << 1));
// Binary right shift (>>)
System.out.println("a >> 1 = " + (a >> 1));
// Logical AND (&&)
System.out.println("(a > 0) && (b > 0) = "
+ ((a > 0) && (b > 0)));
// Logical OR (||)
System.out.println("(a > 0) || (b > 0) = "
+ ((a > 0) || (b > 0)));
// Equal to (==)
System.out.println("a == b = " + (a == b));
// Not equal to (!=)
System.out.println("a != b = " + (a != b));
// Greater than (>)
System.out.println("a > b = " + (a > b));
// Less than (<)
System.out.println("a < b = " + (a < b));
// Greater than or equal to (>=)
System.out.println("a >= b = " + (a >= b));
// Less than or equal to (<=)
System.out.println("a <= b = " + (a <= b));
}
}
Outputa + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
a & b = 0
a | b = 15
a ^ b = 15
a << 1 = 20
a >> 1 = 5
(a > 0) && (b > 0) = true
(a > 0) || (b > 0) = true
a == b = false
a != b = true
a > b = true...
Binary Operator in Python:
Here are the implementation of Binary Operator in Python language:
Python
a = 10
b = 5
# Binary addition (+)
print("a + b =", a + b)
# Binary subtraction (-)
print("a - b =", a - b)
# Binary multiplication (*)
print("a * b =", a * b)
# Binary division (/)
print("a / b =", a / b)
# Binary modulo (%)
print("a % b =", a % b)
# Binary bitwise AND (&)
print("a & b =", a & b)
# Binary bitwise OR (|)
print("a | b =", a | b)
# Binary bitwise XOR (^)
print("a ^ b =", a ^ b)
# Binary left shift (<<)
print("a << 1 =", a << 1)
# Binary right shift (>>)
print("a >> 1 =", a >> 1)
# Logical AND (and)
print("(a > 0) and (b > 0) =", (a > 0) and (b > 0))
# Logical OR (or)
print("(a > 0) or (b > 0) =", (a > 0) or (b > 0))
# Equal to (==)
print("a == b =", a == b)
# Not equal to (!=)
print("a != b =", a != b)
# Greater than (>)
print("a > b =", a > b)
# Less than (<)
print("a < b =", a < b)
# Greater than or equal to (>=)
print("a >= b =", a >= b)
# Less than or equal to (<=)
print("a <= b =", a <= b)
Output('a + b =', 15)
('a - b =', 5)
('a * b =', 50)
('a / b =', 2)
('a % b =', 0)
('a & b =', 0)
('a | b =', 15)
('a ^ b =', 15)
('a << 1 =', 20)
('a >> 1 =', 5)
('(a > 0) and (b > 0) =', True)
('(a > 0) o...
Binary Operator in C#:
Here are the implementation of Binary Operator in C# language:
C#
using System;
class Program {
static void Main()
{
int a = 10;
int b = 5;
// Binary addition (+)
Console.WriteLine("a + b = " + (a + b));
// Binary subtraction (-)
Console.WriteLine("a - b = " + (a - b));
// Binary multiplication (*)
Console.WriteLine("a * b = " + (a * b));
// Binary division (/)
Console.WriteLine("a / b = " + (a / b));
// Binary modulo (%)
Console.WriteLine("a % b = " + (a % b));
// Binary bitwise AND (&)
Console.WriteLine("a & b = " + (a & b));
// Binary bitwise OR (|)
Console.WriteLine("a | b = " + (a | b));
// Binary bitwise XOR (^)
Console.WriteLine("a ^ b = " + (a ^ b));
// Binary left shift (<<)
Console.WriteLine("a << 1 = " + (a << 1));
// Binary right shift (>>)
Console.WriteLine("a >> 1 = " + (a >> 1));
// Logical AND (&&)
Console.WriteLine("(a > 0) && (b > 0) = "
+ ((a > 0) && (b > 0)));
// Logical OR (||)
Console.WriteLine("(a > 0) || (b > 0) = "
+ ((a > 0) || (b > 0)));
// Equal to (==)
Console.WriteLine("a == b = " + (a == b));
// Not equal to (!=)
Console.WriteLine("a != b = " + (a != b));
// Greater than (>)
Console.WriteLine("a > b = " + (a > b));
// Less than (<)
Console.WriteLine("a < b = " + (a < b));
// Greater than or equal to (>=)
Console.WriteLine("a >= b = " + (a >= b));
// Less than or equal to (<=)
Console.WriteLine("a <= b = " + (a <= b));
}
}
Outputa + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
a & b = 0
a | b = 15
a ^ b = 15
a << 1 = 20
a >> 1 = 5
(a > 0) && (b > 0) = True
(a > 0) || (b > 0) = True
a == b = False
a != b = True
a > b = True...
Binary Operator in Javascript:
Here are the implementation of Binary Operator in Javascript language:
JavaScript
let a = 10;
let b = 5;
// Binary addition (+)
console.log("a + b =", a + b);
// Binary subtraction (-)
console.log("a - b =", a - b);
// Binary multiplication (*)
console.log("a * b =", a * b);
// Binary division (/)
console.log("a / b =", a / b);
// Binary modulo (%)
console.log("a % b =", a % b);
// Binary bitwise AND (&)
console.log("a & b =", a & b);
// Binary bitwise OR (|)
console.log("a | b =", a | b);
// Binary bitwise XOR (^)
console.log("a ^ b =", a ^ b);
// Binary left shift (<<)
console.log("a << 1 =", a << 1);
// Binary right shift (>>)
console.log("a >> 1 =", a >> 1);
// Logical AND (&&)
console.log("(a > 0) && (b > 0) =", (a > 0) && (b > 0));
// Logical OR (||)
console.log("(a > 0) || (b > 0) =", (a > 0) || (b > 0));
// Equal to (==)
console.log("a == b =", a == b);
// Not equal to (!=)
console.log("a != b =", a != b);
// Greater than (>)
console.log("a > b =", a > b);
// Less than (<)
console.log("a < b =", a < b);
// Greater than or equal to (>=)
console.log("a >= b =", a >= b);
// Less than or equal to (<=)
console.log("a <= b =", a <= b);
Outputa + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
a & b = 0
a | b = 15
a ^ b = 15
a << 1 = 20
a >> 1 = 5
(a > 0) && (b > 0) = true
(a > 0) || (b > 0) = true
a == b = false
a != b = true
a > b = true...
Best Practices of Binary Operator:
- Use Parentheses for Clarity: Use parentheses to clarify the order of operations and improve readability, especially when combining multiple operators.
- Avoid Implicit Type Conversion: Be explicit with type conversions to prevent unintended behavior and maintain code clarity. Explicitly cast data types when necessary.
- Use Descriptive Variable Names: Choose meaningful variable names that indicate the purpose of operands and results when performing operations.
Conclusion:
Binary operators are the backbone of programming, enabling developers to perform a myriad of operations on data. Understanding their syntax and applications across different programming languages is essential for crafting efficient and expressive code. Whether you're dealing with arithmetic, bitwise operations, or relational comparisons, binary operators offer the flexibility and power needed to navigate the complexities of programming tasks.
Similar Reads
What are Operators in Programming?
Operators in programming are essential symbols that perform operations on variables and values, enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types. Operators in Programming Table of
15+ min read
Bitwise OR Operator (|) in Programming
In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise OR operator (|). In this article, weâll discuss the Bitwise OR operator, its syntax, properties, applications, and optimization techniques, and concl
5 min read
Bitwise AND operator in Programming
In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise AND operator (&). In this article, we'll dive deep into what is Bitwise AND operator, its syntax, properties, applications, and optimization tech
6 min read
Bitwise XOR Operator in Programming
Bitwise XOR Operator is represented by the caret symbol (^). It is used to perform a bitwise XOR operation on the individual bits of two operands. The XOR operator returns 1 if the corresponding bits in the two operands are different, and 0 if they are the same.Table of Content What is Bitwise XOR?B
5 min read
Logical AND operator in Programming
In programming, Logical operators play a crucial role in manipulating individual data. One of the fundamental logical operators is the Logical AND operator(&&).In this article, weâll discuss what is a Logical AND operator, its syntax, properties, applications, and optimization techniques, an
5 min read
Right Shift Operator (>>) in Programming
Right shift operator (>>), commonly found in programming languages, including C, C++, Java, and others, is used to shift the bits of a number to the right by a specified number of positions. Each shift moves all bits in the operand to the right by the number of positions indicated by the right
15+ min read
Binary Exponentiation for Competitive Programming
In competitive programming, we often need to do a lot of big number calculations fast. Binary exponentiation is like a super shortcut for doing powers and can make programs faster. This article will show you how to use this powerful trick to enhance your coding skills. Table of ContentWhat is Binary
15+ min read
Bitwise Left Shift(<<) Operator in Programming
Bitwise operators play a significant role in manipulating individual bits within binary data. Among these operators, the Bitwise Left Shift (<<) operator is used for shifting the bits of a number to the left by a specified number of positions. In this blog post, we'll explore the definition, s
5 min read
Binary Operation
Binary Operation is an operation defined for any set S such that it takes two elements from S as input and produces a single element in S as output. As the name suggests, binary operations require at least two inputs as it is defined from the cartesian product of set to set itself.In this article, w
7 min read
Bitwise Hacks for Competitive Programming
Prerequisite: It is recommended to refer Interesting facts about Bitwise Operators How to set a bit in the number 'num': If we want to set a bit at nth position in the number 'num', it can be done using the 'OR' operator( | ).  First, we left shift '1' to n position via (1<<n)Then, use the 'O
14 min read