Like Tree1Likes
  • 1 Post By Salem

Largest Power of 2 below a number n

This is a discussion on Largest Power of 2 below a number n within the Tech Board forums, part of the Community Boards category; What is an equation to find the largest power of 2 less than or equal to integer n using basic ...

  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 03:02 AM.

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

    Notice anything?
    phantomotap likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    Learn how to compute a binary logarithm, it will help.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    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, 06:53 AM
  2. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 08: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-29-2003, 12:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21