## Introduction

Welcome readers! We hope you are doing great.

** Ruby** is an open-source programming language designed by

**Yukihiro Matsumoto**in the

**mid-1990s**. It was designed with a focus on

**simplicity and programming productivity**. It is a

**high-level, general-purpose programming language**which supports many

**programming paradigms**.

If you want to learn about Ruby, check out our articles on Ruby.

Today, In this article, we will discuss the** Bitwise NOT(~), AND(&), OR(|) and XOR(^) Operations in Ruby **with proper implementation and explanation. So, follow this article till the end.

So, without further ado, letâ€™s start our discussion.

## Bitwise Operations in Ruby

Here in this section, we will discuss different Bitwise operations in Ruby.

### NOT(~) Operation

The** Not(~) **operator is a high precedence unary operator in Ruby. It changes each of the 0 bits in its integer operand to 1 and each of the 1 bits to 0 and produces the binary 1â€™s complement of the number.

The following result can be shown using the truth table,

For any integer x, it changes x to ~x, which is nothing but -x-1.

#### Example

```
a = 6
puts "Complement(Not) Operator result: "
puts(~a)
```

#### Output

```
Complement(Not) Operator result:
-7
```

### AND(&) Operation

The And(&) operator is a medium precedence binary operator in Ruby. It follows the below truth table,

From the truth table, it is clear that the result of the bits is 1 if and only if both the bits are set to 1.

#### Example

```
a = 6
b = 5
puts "Bitwise And(&) operation result: "
puts(a & b)
```

#### Output

```
Bitwise And(&) operation result:
4
```

### OR(|) Operation

The OR(|) operator is also a medium precedence binary operator in Ruby. It follows the below truth table.

From the above truth table, it is clear that we can say that the resultant bit will be 1 if at least one of the bits is set to 1.

#### Example

```
a = 6
b = 5
puts "Bitwise OR(|) operation result: "
puts(a | b)
```

#### Output

```
Bitwise OR(|) operation result:
7
```

### XOR(^) Operation

The XOR(^) operator is a medium precedence operator for integers. The truth table of the XOR(Exclusive-OR) is shown below:

From the above truth table, it is clear that we can say that the resultant bit will be 1 if both the bits are different.

#### Example

```
a = 6
b = 5
puts "Bitwise XOR(^) operation result: "
puts(a ^ b)
```

#### Output

```
Bitwise XOR(^) operation result:
3
```