Left Shift and Right Shift
C++
Medium
3 views
Problem Description
Shift bits left or right to multiply/divide by 2.
Real Life: Fast multiplication/division by powers of 2.
Step-by-Step Logic:
1. Left shift (): moves bits right, divides by 2
3. Each shift is one position
4. Very fast operation
Official Solution
void operator_q7_bit_shift() {
int num = 5; // Binary: 101
cout << "Original number: " << num << endl;
cout << "Left shift by 1 (num << 1): " << (num << 1) << endl; // 10 (multiply by 2)
cout << "Left shift by 2 (num << 2): " << (num << 2) << endl; // 20 (multiply by 4)
cout << "Right shift by 1 (num >> 1): " << (num >> 1) << endl; // 2 (divide by 2)
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!