**Swift Bitwise and Bit Shift Operators**: In this tutorial, we will learn about the bitwise operator and various types of shift operators in Swift with the assistance of examples.

Bitwise operators work on bits and perform bit by bit operation. The truth tables for &, |, and ^ are as follows −

p | q | p & q | p | q | p ^ q |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

1 | 0 | 0 | 1 | 1 |

Bitwise operators perform procedures on integer information at the individual piece level. These activities incorporate testing, setting, or moving the actual pieces. For instance,

a & b a | b

In the example, $ | are bitwise operators.

Here’s the list of different bitwise operators remembered for Swift

Operators | Name | Example |

& | Bitwise AND | a & b |

| | Bitwise OR | a | b |

^ | Bitwise XOR | a ^ b |

~ | Bitwise NOT | ~ a |

<< | Bitwise Shift Left | a << b |

>> | Bitwise Shift Right | a >> b |

Contents

- 1 Bitwise AND Operator
- 2 Example 1: Bitwise AND Operator
- 3 Bitwise OR Operator
- 4 Example 2: Bitwise OR Operator
- 5 Bitwise XOR Operator
- 6 Example 3: Bitwise XOR Operator
- 7 Bitwise NOT Operator
- 8 2’s complement
- 9 Example 4: Bitwise NOT Operator
- 10 Left Shift Operator
- 11 Example 5: Left Shift Operator
- 12 Right Shift Operator
- 13 Example 5: Right Shift Operator

## Bitwise AND Operator

The bitwise and & operator returns 1 if and just if both the operands are 1. Else, it brings 0 back.

The bitwise AND procedure on a and b can be addressed in the table underneath:

a | b | a & b |

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

Note:The table above is known as “Truth Table” for the bitwise AND operator.

Let’s take a look at the bitwise AND operation of two integers 12 and 25:

12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise AND Operation of 12 and 25 00001100 & 00011001 _____________ 00001000 = 8 (In Decimal)

## Example 1: Bitwise AND Operator

var a = 12 var b = 25 var result = a & b print (result) // 8

In the above example, we have proclaimed two variables a and b. Here, notice the line,

var result = a & b

Here, we are performing bitwise AND operation between a and b.

## Bitwise OR Operator

The bitwise OR | operator returns 1 if in any event one of the operands is 1. Else, it brings 0 back.

The bitwise OR operation on a and b can be addressed in the table beneath:

a | a | a | b |

0 | 0 | 0 |

0 | 1 | 1 |

1 | 1 | 1 |

1 | 0 | 1 |

Let us look at the bitwise OR operation of two integers 12 and 25:

12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ____________ 00011101 = 29 (In decimal)

## Example 2: Bitwise OR Operator

var a = 12 var b = 25 var result = a | b print(result) // 29

Here, we are performing bitwise OR between 12 and 25.

## Bitwise XOR Operator

The bitwise XOR ^ operator returns 1 if and just on the off chance that one of the operands is 1. Be that as it may, assuming both the operands are 0, or if both are 1, the outcome is 0.

The bitwise XOR operation on a and b can be addressed in the table beneath:

a | b | a ^ b |

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

Let us look at the bitwise XOR operation of two integers 12 and 25:

12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise XOR Operation of 12 and 25 00001100 ^ 00011001 ____________ 00010101 = 21 (In decimal)

## Example 3: Bitwise XOR Operator

var a = 12 var b = 25 var result = a ^ b print(result) // 21

Here, we are performing bitwise XOR between 12 and 25.

## Bitwise NOT Operator

The bitwise NOT ~ operator inverts the piece( 0 becomes 1, 1 gets 0).

Note that the bitwise NOT of any integer N is equivalent to – (N + 1). For instance,

Consider an integer 35. According to the standard, the bitwise NOT of 35 ought to be – (35 + 1) = – 36. Presently, how let’s see if we get the correct answer or not.

35 = 00100011 (In Binary) // Using bitwise NOT operator ~ 00100011 ________ 11011100

In the above example, bitwise NOT of 00100011 is 11011100. Here, in the event that we convert the outcome into decimal we get 220.

Nonetheless, note that we can’t straightforwardly change over the outcome into decimal and get the desired output. This is on the grounds that the binary outcome 11011100 is additionally comparable to – 36.

To comprehend this we first need to calculate the binary output of – 36. We use 2’s complement to calculate the binary of negative integers.

## 2’s complement

The 2’s complement of a number N gives – N. It is computed by reversing the bits(0 to 1 and 1 to 0) and afterward adding 1. For instance,

36 = 00100100 (In Binary) 1's Complement = 11011011 2's Complement : 11011011 + 1 ________ 11011100

Here, we can see the 2’s complement of a 36(i.e. – 36) is 11011100. This worth is identical to the bitwise complement of 35 that we have calculated in the previous section.

Consequently, we can say that the bitwise complement of 35 = – 36.

## Example 4: Bitwise NOT Operator

var b = 12 var result = ~b print(result) // -13

In the above example, we have performed the bitwise NOT operation on 12.

The bitwise complement of 12 = - (12 + 1) = -13 i.e. ~12 = -13

This is actually what we got in the output.

## Left Shift Operator

The left shift operator moves all pieces towards the left by a specified number of pieces. It is indicated by <<.

- We have a 4-digit number. At the point when we play out a 1 digit left shift operation on it, each piece is moved to one side by 1 bit.
- Thus, the furthest left piece is disposed of, while the right-most piece stays empty. This opportunity is replaced by 0.

## Example 5: Left Shift Operator

var a = 3 var result = a << 2 print(result) // 12

In the above example, we have created a variable `a`

with the value `3`

. Notice the statement

var result = a << 2

Here, we are performing 2 pieces left shift operation on a.

## Right Shift Operator

The right shift operator moves all pieces towards the right by a specific number of determined pieces. It is indicated by >>.

As we can see from the picture above,

- We have a 4-digit number. At the point when we play out a 1-bit right shift operation on it, each piece is moved to the right by 1 bit.
- Subsequently, the right-most piece is disposed of, while the furthest left piece stays empty. This opening is replaced by 0 for unsigned numbers.
- For signed numbers, the sign piece (0 for positive number, 1 for negative number) is used to fill the abandoned piece positions.

Note:Signed number addresses both positive and negative integers while an unsigned integer just addresses positive integers.

## Example 5: Right Shift Operator

var a = 4 var result = a >> 2 print(result) // 1 a = -4 result = a >> 2 print(result) // -1

In the above example, we are performing 2 bits right shift operations on qualities 4 and – 4.

As you can see the outcome is different for 4 and – 4. This is on the grounds that 4 is an unsigned integer so the opportunity is filled with 0 and – 4 is a negative signed number so the opening is filled with 1.

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.