Re: how to specify power of number




"Yanb" <Yanb@xxxxxxxxxxxxxxxx> wrote in message
news:fu9q89$kem$1@xxxxxxxxxxx
Hi, I'm new to c, so please excuse, if this is silly ;-)

I made a C function, which takes IP adress from string and converts to
unsigned long int. Everything
works, but I found that the "counting" part part works only with this:

numericip=atoi(textip[0])*256*256*256+atoi(textip[1])*256*256+atoi(textip[2])*256+atoi(textip[3]);

When I wrote *(256^3), I got fake results. I'd like to use something more
sophisticated, instead of 256*256
*256, which does look silly ;-) (Just curious, what if I'd ever need to
make 100th power of 256 ;-) )

You would have to use pow(256,3) which uses floating point arithmetic -- not
recommended, even though the compiler is likely to optimise.

Your code is fine as it is, the compiler will optimise 256*256*256 to
*16777216 or <<24. Or just write those in yourself.

--
Bart


.



Relevant Pages

  • Re: RAD vs. performance
    ... be no slower than the specialized version, simply because the compiler ... so your compile times must go through the roof in order to optimise ... but they don't because you rarely need that abstraction. ... I'd have an int argument and generalise the result using ...
    (comp.lang.misc)
  • Re: RAD vs. performance
    ... underused and undersupported (by the compiler). ... away the unwanted abstraction that you have imposed upon us. ... the type int -> int and the type checker will unify this type with any ... equivalent to the case of your compiler being unable to optimise away ...
    (comp.lang.misc)
  • Re: C99 exact width integers usage - informal survey
    ... You might declare it an ... but the compiler will optimise it to ... the maximum int size. ...
    (comp.arch.embedded)
  • Re: How does printf() works
    ... Where unknownlibrarycall is some library the compiler does not know ... The compiler can optimise this to: ...
    (comp.lang.c)
  • Re: RAD vs. performance
    ... whereas my compiler can optimize them ... optimise away your abstraction because you have thrown away the type ... check as effectively because you do not have the type information. ... away the unwanted abstraction that you have imposed upon us. ...
    (comp.lang.misc)