Calculating Binary

Discussion in 'Scripting & Programming' started by Fergal1982, Oct 11, 2010.

  1. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    This doesn't fit neatly into the programming section, since the technique applies to multiple sections, but since I'm looking at it from a programming perspective, I'll stick it here.

    Does anyone have a quick technique for calculating the maximum possible value given a binary length (such as 2^32 for instance)? I could just try to memorise the maximum values, but I think it would be better for me to have a technique for calculating it.

    It doesnt have to be spot on I suppose, just a rough indicator.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  2. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,306
    503
    259
    You said what it is, its 0 to 2powN where N is number of bits for unsigned.

    If its a signed twos complement number its -2pow(N-1) to 2pow(N-1)-1.

    Floating point is more complicated and is expressed as a mantissa and exponent, look into the IEEE specs for double and single precision floating point for details.
     
  3. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Yes, there is a quick way. If you wish to find the power of 2 for a particular number n you do:

    log(n) / log(2)

    (base 10 logarithm) and to reverse the process you do as dmarsh said.

    Hi and long time no read! :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.