Skip to content
Algorithms
  • Home
  • IDE
  • Site Map
  • Difficulty Level
    • Beginner
    • Intermediate
    • Expert
  • Interviews
    • Amazon Questions
    • Goldman Sachs
    • Google Interview
    • Microsoft Interview
    • Apache
    • NetApp
    • Adobe
    • Facebook
    • NVIDIA
    • Oracle
Bit Manipulation

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

August 31, 2019June 29, 2018 by Sumit Jain

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:

Output:

Number 48 Divided by 2^4 is: 3

Read about –

  • Left and Right shift operators.
  • Multiply with power of 2 without using pow() or * operator

Related Posts:

  • Multiply with power of 2 without using pow() or * operator
  • Left Shift () Operators
  • Number's Complement - 2 Approaches
  • Check if Given Number is power of 2.
  • Decimal to Binary
  • Swap two numbers using Bitwise XOR Operator
  • Convert Infix to Postfix Expression
  • Convert Number to base 3 String Representation
  • Evaluation of Infix expressions
  • Count Set bits in a given Number
  • Evaluation of Postfix Expressions (Polish Postfix notation) | Set 1
  • Evaluation of Postfix Expressions (Polish Postfix notation) | Set 2
  • Check if the given number is Armstrong number or not
  • Find Sum of all Digits of a Number
  • Find Factorial of a given Number
Categories Beginner, Bit Manipulation, Maths, MISC Tags Beginner 1 Comment
Post navigation
Maximum Difference between two elements in array – Largest Gap Problem
Fizz Buzz Challenge – Java Implementation



More Problems

  • Evaluation of Postfix Expressions (Polish Postfix notation) | Set 1
  • Evaluation of Postfix Expressions (Polish Postfix notation) | Set 2
  • Check if the given number is Armstrong number or not
  • Find Sum of all Digits of a Number
  • Find Factorial of a given Number
  • Calculate Log2n without using built-in function
  • Evaluation of Prefix Expressions (Polish Notation) | Set 1
  • Evaluation of Prefix Expressions (Polish Notation) | Set 2
  • Find all the Armstrong numbers in the given range
  • Graph Implementation – Adjacency Matrix | Set 3




Subscribe ( No Spam!!)

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,417 other subscribers

Recent Posts

  • Rank Array Elements
  • Three Consecutive Odd Numbers
  • Non-decreasing Array with one allowed change
  • Duplicate zero’s without expanding the array.
  • Maximum Depth of Valid Nested Parentheses
  • Decimal to Binary
  • Maximum Consecutive Ones
  • Duplicate even elements in an array
  • Minimum Increments to make all array elements unique
  • Add digits until number becomes a single digit






© 2021 Algorithms • Powered by GeneratePress