# Operators in Ruby

Ruby supports a wide variety of operators

- Unary Operators
- Arithmetic Operators
- Bitwise Operators
- Logical Operator
- Ternary Operator
- Comparison Operator
- Range Operator

Let us look into each type of operator in detail

## Arithmetic Operator

These operators perform necessary arithmetic operations on the operands. These computations include all the operations that are defined in the table below.

Let x and y be two operands

Operator | Operation | Expression | Description |
---|---|---|---|

+ | Addition | a+b | Returns sum of a & b |

- | Subtraction | a-b | Returns difference of a&b |

/ | Division | a/b | Returns quotient of a&b |

* | Multiplication | a*b | Returns product of a&b |

** | Exponent | a**b | Returns a to the power b |

% | Modulus | x%y | Returns remainder of a&b |

## Comparison Operators

Comparison operators unlike arithmetic operators only compare two values and return true or false as the result

Operator | Operation | Expression | x=y | x>y | x<y<y< th=""></y<> |
---|---|---|---|---|---|

== | Equal to | x == y | True | False | False |

!= | Not Equal to | x != y | False | True | True |

<= | Less than equal to | x <= y | True | False | True |

>= | Greater than Equal to | x >= y | True | True | False |

< | Less than | x < y | False | False | True |

> | Greater than | x > y | False | True | False |

<==> | Combined Comparision | x <==> y | 0 | 1 | -1 |

## Bitwise Operator

Bitwise operators perform operations on a bit level. After converting the value into binary form, the bitwise operators perform operations on corresponding bits of the operands.

Operator | Operation | Expression | Description |
---|---|---|---|

& | Bitwise AND | a&b | Returns 1 when both corresponding bits are 1 |

| | Bitwise OR | a|b | Returns 1 when either corresponding bits are 1 |

^ | Bitwise XOR | a^b | Returns 0 only when both bits are 1 |

~ | Compliment | ~a | Flips all the bits in the binary form of the operand |

<< | Left Shift | a<<b< td=""></b<> | shifts all the bits to the left by one position* |

>> | Right Shift | a>>b | shifts all the bits to the right by one position* |

*In Left/Right shifts the leftmost/rightmost bit is discarded and 0 is inserted towards the other end.

## Ternary Operator

Operator | Description |
---|---|

?: | Conditional expression |

### Related Topics

## Join Our Data Science Bootcamp

With Job Assistance