Bitwise Left Shift Operation:
From: | To: |
The bitwise left shift operation moves all bits in a number to the left by a specified number of positions. This is equivalent to multiplying the number by 2 raised to the power of the shift amount.
The left shift operation follows this formula:
Example: 5 << 2 = 20
Syntax: In Python, the left shift operator is represented by <<
.
Example Code:
num = 5 bits = 2 result = num << bits # Returns 20
Tips: Enter any integer number and the number of bits to shift. The calculator will show the result in decimal, binary, and hexadecimal formats.
Q1: What happens to bits shifted past the leftmost position?
A: They are discarded. Python integers have arbitrary precision, so the number will grow to accommodate the shift.
Q2: Is left shift equivalent to multiplication by 2?
A: Yes, for each bit shifted, the number is multiplied by 2 (when no bits are lost).
Q3: Can I shift negative numbers?
A: Yes, but the sign is preserved. The left shift operation works on the two's complement representation.
Q4: What's the difference between arithmetic and logical left shift?
A: In Python, they're the same. Some languages differentiate, but Python's << performs arithmetic shift.
Q5: Is there a limit to how many bits I can shift?
A: In theory, no, but extremely large shifts will consume significant memory.