”;
Bitwise Operators in Swift
Bitwise operators are commonly used when we want to perform operations on the bits of the specified number. These operators are used in low-level programming, to do bit-level calculations or in communication networks to send data in bits. We can apply bitwise operators on integer types.
These operators do not cause overflow because the result of bitwise operators is always within the range of the given numeric type. Swift supports the following bitwise operators −
Operator | Name | Example |
---|---|---|
~ | Bitwise NOT Operator | ~(11011) = (00100) |
& | Bitwise AND Operator | (110011) & (101101) = 100001 |
| | Bitwsie OR Operator | (100011) | (10111) = 101111 |
^ | Bitwise XOR operator | (100011) ^ (10111) = 00100 |
<< | Bitwise Left Shift Operator | (100011) << 2 = 10001100 |
>> | Bitwise Right Shift Operator | (100011) >> 2 = 1000. |
Bitwise NOT Operator in Swift
A bitwise NOT operator(~) is used to invert all the given bits(which converts 0’s into 1’s or 1’s into 0’s). Or we can say that it returns the one”s complement of the given bits.
For example, we have 1100011 bits so after using the bitwise NOT operator we will get 0011100 bits as an output. It is a prefix operator and is used just before the variable it will operate on.
Syntax
Following is the syntax of the bitwise NOT operator −
~VariableName
Example
Swift program to calculate one’s complement using bitwise NOT operator.
import Foundation var num : UInt8 = 0b00011111 // Finding one''s complement using bitwise NOT operator var result = ~(num) print("Original bits:", String(num, radix: 2)) print("One''s complement:", String(result, radix: 2))
Output
Original bits: 11111 One''s complement: 11100000
Bitwise AND Operator in Swift
A bitwise AND operator “&” is used to perform the AND operation on every bit of the given two operands. Or we can say that it is used to combine the bits of the given two numbers and return a new number whose bits are set to 1 only if both the input bits are 1. For example, A = 110011 and B = 101101, so after performing bitwise AND we get 100001. The truth table is −
P | Q | P & Q |
---|---|---|
0 | 0 | 0 |
1 | 0 | 0 |
0 | 1 | 0 |
1 | 1 | 1 |
Syntax
Following is the syntax of the bitwise AND operator −
Operand1 & Operand2
Example
Swift program to perform bitwise AND operation.
import Foundation var num1 : UInt8 = 0b1000101 var num2 : UInt8 = 0b1100011 // Performing bitwise AND operation var result = num1 & num2 print("Result:", String(result, radix: 2))
Output
Result: 1000001
Bitwise OR Operator in Swift
A bitwise OR operator “|” is used to perform the OR operation on every bit of the given two operands. Or we can say that it is used to compare the bits of the given two numbers and return a new number whose bits are set to 1 if any of the input bits are 1.
For example, A = 100011 and B = 10111, so after performing bitwise OR we get 101111. The truth table is −
P | Q | P | Q |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 1 |
Syntax
Following is the syntax of the bitwise OR operator −
Operand1 | Operand2
Example
Swift program to perform bitwise OR operation.
import Foundation var num1 : UInt8 = 0b1010001 var num2 : UInt8 = 0b1100011 // Performing bitwise OR operation var result = num1 | num2 print("Result:", String(result, radix: 2))
Output
Result: 1110011
Bitwise XOR Operator in Swift
A bitwise XOR operator “^” is used to perform the XOR operation on every bit of the given two operands. Or we can say that it is used to compare the bits of the given two numbers and return a new number whose bits are set to 1 if one bit in input is 0 and another bit is 1 or vice versa
For example, A = 100011 and B = 10111, so after performing bitwise XOR we get 00100. The truth table is −
P | Q | P ^ Q |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 0 |
Syntax
Following is the syntax of the bitwise XOR operator −
Operand1 ^ Operand2
Example
Swift program to perform bitwise XOR operation.
import Foundation var num1 : UInt8 = 0b1011001 var num2 : UInt8 = 0b1100011 // Performing bitwise XOR operation var result = num1 ^ num2 print("Result:", String(result, radix: 2))
Output
Result: 111010
Bitwise Left Shift Operator in Swift
A bitwise left shift operator “<<” is used to shift all the bits of the given number on the left side by a given number of places. For example, A = 100011 so after shifting the bits by 2 places we get 10001100.
While shifting unsigned integers on the left side, if any bits move beyond the integer bound then they will have discarded. Also when we shift bits from left to right a space is left after the movement of the original bits so that space is filled with zeros.
In contrast, signed integers are shifted in two different ways. The positive integers (if the sign bit is 0) will store just like unsigned integers. In contrast, the negative integers (if the sign bit is 1) will be stored by subtracting their absolute value from 2 to the power n, where n represents the number of value bits, it is also known as two’s complement.
Syntax
Following is the syntax of the bitwise Left Shift operator −
Operand1 << Operand2
Example
Swift program to perform bitwise left shift operation.
import Foundation let number: UInt8 = 42 // Perform a bitwise left shift operation // Here the bits of a given number are shifted by 3 places on the left side let shiftedNum = number << 3 print("Original Number: (number)") print("Shifted Number: (shiftedNum)")
Output
Original Number: 42 Shifted Number: 80
Bitwise Right Shift Operator in Swift
A bitwise right shift operator “>>” is used to shift all the bits of the given number on the right side by a given number of places. For example, A = 100011 so after shifting the bits by 2 places we get 1000.
As we know integers are of two types signed and unsigned. So when we shift unsigned integers on the right side, if any bits move beyond the integer bound then they will have discarded. Also when we shift bits from right to left a space is left after the movement of the original bits so that space is filled with zeros.
At the same time, signed integers are shifted in two different ways. The positive integers (if the sign bit is 0) will store just like unsigned integers and the negative integers (if the sign bit is 1) will store by subtracting their absolute value from 2 to the power n, where n represents the number of value bits.
Syntax
Following is the syntax of the bitwise right shift operator −
Operand1 >> Operand2
Example
Swift program to perform bitwise right shift operation.
import Foundation let number: UInt8 = 32 // Perform a bitwise right shift operation // Here the bits of a given number are shifted by 2 places on the right side let shiftedNum = number >> 2 print("Original Number: (number)") print("Shifted Number: (shiftedNum)")
Output
Original Number: 32 Shifted Number: 8
”;