Estimated reading time: 4 minutes

In Julia, bitwise operators are used to manipulate the binary representation of integers. They operate on the individual bits of a number and perform logical operations such as AND, OR, XOR, and NOT.

## What is bitwise?

Bitwise refers to the manipulation of binary digits (bits) at the level of individual bits.

Bitwise operations involve comparing, shifting, and otherwise manipulating the individual bits that make up a binary number or binary data. These operations are commonly used in computer programming for tasks such as data encryption, compression, and optimization.

Bitwise operators are used to perform logical operations such as AND, OR, XOR, and NOT on binary data. These operators take two input values, each of which is treated as a sequence of bits.

The operators perform the specified operation on each pair of corresponding bits and produce a new sequence of bits as the output.

## How can I calculate the bit of an integer in Julia?

In Julia, you can use the `bitstring()`

function to get the binary representation of a character. The `bitstring()`

function returns a string that represents the bits of the input argument. Each character in the string corresponds to a bit in the binary representation, with the most significant bit first.

To get the number of bits used to represent a character, you can use the `sizeof()`

function.

The `sizeof()`

function returns the number of bytes used to represent the input argument.

The default type for an integer literal depends on whether the target system has a 32-bit architecture or a 64-bit architecture:

```
# 32-bit system:
julia> typeof(1)
Int32
# 64-bit system:
julia> typeof(1)
Int64
```

## What are the bitwise operators in Julia?

- Bitwise AND (&): This operator performs a bitwise AND operation between two integers. It returns a new integer where each bit is set to 1 only if the corresponding bits in both integers are 1.

Example:

```
julia> a=1
1
julia> b=1
1
julia> c= a & b
1
```

2. Bitwise OR (|): This operator performs a bitwise OR operation between two integers. It returns a new integer where each bit is set to 1 if either of the corresponding bits in both integers is 1.

Example:

```
julia> a = 5 # binary representation: 101
5
julia> b = 3 # binary representation: 011
3
julia> c = a | b # binary representation: 111 (decimal value: 7)
7
```

```
In the above Example binary representation: 101 is calculated as follows:
To calculate the binary representation of a decimal number, you can use the process of repeated division by 2. Here's how you can calculate the binary representation of the decimal number 5:
Start with the decimal number 5.
Step 1. Divide 5 by 2, and write down the remainder. In this case, 5 divided by 2 is 2 with a remainder of 1, so write down "1".
Step 2. Divide the quotient from step 1 (which is 2 in this case) by 2, and write down the remainder. The quotient is 1 with a remainder of 0, so write down "0".
Step 3. Divide the quotient from step 2 (which is 1 in this case) by 2, and write down the remainder. The quotient is 0 with a remainder of 1, so write down "1".
There are no more quotients to divide, so the binary representation of 5 is the reverse of the remainders: 101.
Therefore, the binary representation of the decimal number 5 is 101.
```

3. Bitwise XOR ($\oplus$): This operator performs a bitwise XOR (exclusive OR) operation between two integers. It returns a new integer where each bit is set to 1 only if the corresponding bits in both integers are different.

Example:

```
julia> a = 5 # binary representation: 101
5
julia> b = 3 # binary representation: 011
3
julia> c = a ⊻ b # binary representation: 110 (decimal value: 6)
6
```

4. Bitwise NOT (~): This operator performs a bitwise NOT operation on an integer. It returns a new integer where each bit is flipped (i.e., 1s become 0s and vice versa).

Example:

```
julia> a = 5 # binary representation: 101
5
julia> b = ~a # binary representation: 11111111111111111111111111111010 (decimal value: -6)
-6
```

5. Bitwise left shift (<<): This operator shifts the bits of an integer to the left by a specified number of positions. The leftmost bits are shifted out and zeros are shifted in from the right.

Example:

```
julia> a = 5 # binary representation: 101
5
julia> b = a << 2 # binary representation: 10100 (decimal value: 20)
20
```

6. Bitwise right shift (>>): This operator shifts the bits of an integer to the right by a specified number of positions. The rightmost bits are shifted out and zeros are shifted in from the left.

Example:

```
julia> a = 5 # binary representation: 101
5
julia> b = a >> 2 # binary representation: 1 (decimal value: 1)
1
```

Click here to see how a binary number is calculated.