Bash bitwise operators

Like many other programming languages, Bash supports a set of bitwise operators that enable you to perform operations on individual bits within a data value.
Bash bitwise operators

On this page

Bitwise operators allow you to perform low-level manipulations on binary data. These operations can be particularly useful in various scenarios, such as working with system configurations, network programming, and data processing. In this article, we will explore the different Bash bitwise operators and their practical applications, equipping you with the knowledge to harness their potential in your Bash scripts.

Understanding Bits and Bytes

Before we jump into the operators, let's quickly review the concept of bits and bytes. Computers fundamentally store and process information using a binary system, which means they represent data using only two digits: 0 and 1. These individual digits are called bits.

Each bit position holds a power of two, increasing right to left. For instance, the decimal number 13 has the following binary representation:

1101  (8 + 4 + 0 + 1)

A byte is a group of 8 bits, the basic unit of information in a computer. Each byte can represent a number from 0 to 255 (in decimal) or a specific character, such as a letter or a symbol.

What are Bitwise Operators

Like many other programming languages, Bash supports a set of bitwise operators that enable you to perform operations on individual bits within a data value. These operators work by manipulating the binary representation of the data, allowing you to perform various logical operations on the bits.

Bash supports six bitwise operators:

  1. AND (&): The bitwise AND operator performs a logical AND operation on each corresponding bit of the operands.
  2. OR (|): The bitwise OR operator performs a logical OR operation on each corresponding bit of the operands.
  3. XOR (^): The bitwise XOR (exclusive OR) operator performs a logical XOR operation on each corresponding bit of the operands.
  4. NOT (~): The bitwise NOT operator performs a logical NOT operation on each bit of the operand, flipping the bits.
  5. Left Shift (<<): The left shift operator shifts the bits of the left operand to the left by the number of positions specified by the right operand.
  6. Right Shift (>>): The right shift operator shifts the bits of the left operand to the right by the number of positions specified by the right operand.

Let's go through some simple examples to help you understand these operators better.

Bitwise AND (&)

The bitwise AND operator compares each bit of the first operand with the corresponding bit of the second operand. If both bits are 1, the resulting bit is set to 1; otherwise, it is set to 0.

Example:

# Perform bitwise AND on two decimal numbers
a=15     # Binary: 1111
b=7      # Binary: 0111
result=$((a & b))
echo "Bitwise AND of $a and $b: $result" # Output: Bitwise AND of 15 and 7: 7

In this example, the bitwise AND operation is performed on the decimal numbers 15 and 7, which correspond to the binary representations 1111 and 0111, respectively. The resulting value is 7, which is the binary representation 0111.

Bitwise OR (|)

The bitwise OR operator compares each bit of the first operand with the corresponding bit of the second operand. If either or both bits are 1, the resulting bit is set to 1.

Example:

# Perform bitwise OR on two decimal numbers
a=15     # Binary: 1111
b=7      # Binary: 0111
result=$((a | b))
echo "Bitwise OR of $a and $b: $result" # Output: Bitwise OR of 15 and 7: 15

In this example, the bitwise OR operation is performed on the decimal numbers 15 and 7, which correspond to the binary representations 1111 and 0111, respectively. The resulting value is 15, which is the binary representation 1111.

Bitwise XOR (^)

The bitwise XOR (exclusive OR) operator compares each bit of the first operand with the corresponding bit of the second operand. If the bits are different, the resulting bit is set to 1; otherwise, it is set to 0.

Example:

# Perform bitwise XOR on two decimal numbers
a=15     # Binary: 1111
b=7      # Binary: 0111
result=$((a ^ b))
echo "Bitwise XOR of $a and $b: $result" # Output: Bitwise XOR of 15 and 7: 8

In this example, the bitwise XOR operation is performed on the decimal numbers 15 and 7, which correspond to the binary representations 1111 and 0111, respectively. The resulting value is 8, which is the binary representation 1000.

Bitwise NOT (~)

The bitwise NOT operator flips all the bits of the operand, converting 1s to 0s and 0s to 1s.

Example:

# Perform bitwise NOT on a decimal number
a=15     # Binary: 1111
result=$((~a))
echo "Bitwise NOT of $a: $result" # Output: Bitwise NOT of 15: -16

In this example, the bitwise NOT operation is performed on the decimal number 15, which corresponds to the binary representation 1111. The resulting value is -16, which is the binary representation 1111111111111111 (16 bits, all 1s).

Bitwise Left Shift (<<)

The bitwise left shift operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. This effectively multiplies the left operand by 2 raised to the power of the right operand.

Example:

# Perform bitwise left shift on a decimal number
a=8      # Binary: 1000
result=$((a << 2))
echo "Bitwise left shift of $a by 2 positions: $result" # Output: Bitwise left shift of 8 by 2 positions: 32

In this example, the bits of the decimal number 8, which corresponds to the binary representation 1000, are shifted to the left by 2 positions. This results in the binary representation 100000, which is the decimal number 32.

Bitwise Right Shift (>>)

The bitwise right shift operator shifts the bits of the left operand to the right by the number of positions specified by the right operand. This effectively divides the left operand by 2 raised to the power of the right operand.

Example:

# Perform bitwise right shift on a decimal number
a=32     # Binary: 100000
result=$((a >> 2))
echo "Bitwise right shift of $a by 2 positions: $result" # Output: Bitwise right shift of 32 by 2 positions: 8

In this example, the bits of the decimal number 32, which corresponds to the binary representation100000, are shifted to the right by 2 positions. This results in the binary representation 1000, which is the decimal number 8.

Practical Applications of Bitwise Operators

Bitwise operators in Bash can be particularly useful in the following scenarios:

  1. System Configuration: Manipulating system configuration flags or settings stored as bit fields.
  2. Network Programming: Working with network addresses, subnet masks, and other network-related data.
  3. Data Manipulation: Data processing, such as packing and unpacking data, or performing bit-level operations on arrays.
  4. Bit Flags: Setting, clearing, and checking individual bits in a data value for managing bit flags.
  5. Encryption and Decryption: Implementing simple encryption and decryption algorithms.

Summing up

Bitwise operators allow for low-level manipulations on binary data, which can be useful in various scenarios such as system configurations, network programming, and data processing. Understanding these operators and their practical applications can enhance the functionality of Bash scripts.

Subscribe to sysxplore newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!