This post is completed by 1 user

  • 1
Add to List
Beginner

301. Bitwise Trick: Divide by Powers of 2 Without Operators

Objective: Given a number n and k, Calculate n / k2 without using the power function or / operator.

Example:

N = 48, k = 4
N/k2 = 3

Approach: Bit Manipulation

  • Right shift the number N by k.
  • N = 48
  • Bit representation: 0 1 1 0 0 0 0
  • Right shift by k = 4
  • 0 1 1 which is the representation of 3.
 

Output:

Number 48 Divided by 2^4 is: 3

Read about –