# Divide with power of 2 without using pow() or / operator

Objective: Given a number n and k, Calculate n / k2 without using pow() 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.

Java Code:

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

 public class MultiplyByPowerOf2 { // Left shift the number n by k. public static void multiply(int n, int k){ System.out.print("Number " + n + " Multiplied by 2^" + k +" is: "); System.out.println(n<

Output:

```Number 48 Divided by 2^4 is: 3
```

### 1 thought on “Divide with power of 2 without using pow() or / operator”

1. The code seems to be incorrect, it’s multiply instead of divide and left shift instead of right shift

public class MultiplyByPowerOf2 {
// Left shift the number n by k.
public static void multiply(int n, int k){
System.out.print(“Number ” + n + ” Multiplied by 2^” + k +” is: “);
System.out.println(n<<k);
}
public static void main(String[] args) {
int n = 3;
int k = 4;
multiply(n,k);
}
}