Thread: Largest Power of 2 below a number n

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    Largest Power of 2 less than or equal to integer n

    What is an equation to find the largest power of 2 less than or equal to integer n using basic operators, ie, +-*/^ logic gates mod rem?
    Last edited by Blasz; 05-05-2011 at 02:02 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Write out some numbers in binary.
    Write out some powers of two less than that, in binary.

    Notice anything?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Yep, I'm having trouble making that into an equation though.

    I also meant less than or equal to an integer n.

    So basically its 2^(position of leftmost 1 in binary) but I don't know how to make that into an equation just using n and basic operators. This is for VHDL so any operators there are allowed.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Learn how to compute a binary logarithm, it will help.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You can shift left once each cycle, keep a counter, until you get a 1 on the MSB.

    A single cycle circuit may be huge (I'm not sure).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Largest Number
    By lukesowersby in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 05:53 AM
  2. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  3. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  4. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  5. Determining the Largest number??
    By gqchynaboy in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 11:27 PM