The bitwise left-shift operation in Swift moves the bits of a number towards the left according to a given integer value. The vacant space is then occupied by a 0
. It is represented by <<
.
number << a
number
: This is the value on which the bitwise operation is being performed.a
: This is the integer that dictates how many times the bits will shift to the left.The bitwise left shift operation (<<
) returns a number that is equivalent to multiplying the input value by a factor of 2. For example, shifting the bits of an integer to the left once results in double the value (or
// the number on which left shift will be appliedvar number = 20 // binary : 10100// the number of shiftsvar shift = 3var result = 20 << 3print(result) // 160. binary : 10100000
number
variable, which holds the value on which the left shift will be applied.shift
variable, which is the number of left shifts to be applied.result
variable. We can confirm our answer by the following: Free Resources