How to perform the bitwise left-shift operation in Swift
Overview
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 <<.
Syntax
number << a
Parameters
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.
Return value
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
Example
// 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
Explanation
- Line 2: We initialize the
numbervariable, which holds the value on which the left shift will be applied. - Line 5: We initialize the
shiftvariable, which is the number of left shifts to be applied. - Line 7: We perform the left shift operation and store the result in the
resultvariable. We can confirm our answer by the following:
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved