如何指定数字的力量 [英] how to specify power of number
问题描述
我是c的新手,所以请原谅,如果这很傻;-)
我做了一个C函数,它从字符串转换为IP地址并转换to unsigned long int。一切
有效,但我发现计数了part part仅适用于此:
numericip = atoi(textip [0])* 256 * 256 * 256 + atoi(textip [1])* 256 * 256 + atoi(textip [2])* 256 + atoi(textip [3]);
当我写*(256 ^ 3)时,我得到了假结果。我想使用更复杂的东西,而不是256 * 256
* 256,这看起来很傻;-)(只是好奇,如果我需要制作100次幂,该怎么办? 256 ;-))
提前谢谢。
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 ;-) )
Thank you in advance.
推荐答案
Yanb< Ya ** @ whatever.invalidwrites:
Yanb <Ya**@whatever.invalidwrites:
我是c的新手,所以请原谅,如果这很傻;-)
Hi, I''m new to c, so please excuse, if this is silly ;-)
好吧,不是很傻,但我很惊讶你在没有咨询某种参考的情况下开始使用新语言
。
Well, not silly, but I am surprised that you start a new language
without consulting some sort of reference.
我做了一个C函数,它从字符串中获取IP地址并转换为unsigned long int。一切
有效,但我发现计数了part part仅适用于此:
numericip = atoi(textip [0])* 256 * 256 * 256 + atoi(textip [1])* 256 * 256 + atoi(textip [2])* 256 + atoi(textip [3]);
当我写*(256 ^ 3)时,我得到了假结果。我想用一些东西
更复杂,而不是256 * 256
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
^是独家或在C. C不有一个取幂运算符。
大多数C程序员会使用24,16或8位的移位(<<<<<>)来计算你想要的值。
-
Ben。
^ is exclusive or in C. C does not have an exponentiation operator.
Most C programmers would use a shift (<<) by 24, 16 or 8 bits to
compute the value you want.
--
Ben.
" Yanb" < Ya ** @ whatever.invalidwrote in message
news:fu ********** @ aioe.org ...
"Yanb" <Ya**@whatever.invalidwrote in message
news:fu**********@aioe.org...
我是c的新手,所以请原谅,如果这很傻;-)
我做了一个C函数,它从字符串中获取IP地址并转换为
unsigned long int。一切
有效,但我发现计数了part part仅适用于此:
numericip = atoi(textip [0])* 256 * 256 * 256 + atoi(textip [1])* 256 * 256 + atoi(textip [2])* 256 + atoi(textip [3]);
当我写*(256 ^ 3)时,我得到了假结果。我想用更多的东西
复杂,而不是256 * 256
* 256,这看起来很傻;-)(只是好奇,如果我'我需要
使得100的100次幂;-))
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 ;-) )
你必须使用pow(256,3)使用浮点运算 - 不建议使用
,即使编译器可能会优化。
您的代码很好,编译器会优化256 * 256 * 256到
* 16777216或<< 24。或者只是自己写一下。
-
Bart
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
2008年4月18日11:01,Bartc写道:
On 18 Apr 2008 at 11:01, Bartc wrote:
你必须使用使用浮点运算的pow(256,3) - 不是
建议,即使编译器可能会优化。
你的代码很好,编译器会优化256 * 256 * 256到
* 16777216或<< 24。或者只是自己写一些。
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.
对于一般整数幂,一个简单的square-and-multiply算法是一个好的赌注,例如
>
unsigned long long power(unsigned a,unsigned b)
{
unsigned long long r,bit,pow;
for(bit = r = 1,pow = a; bit< = b; bit<<<< = 1,pow * = pow)
if(b& bit)
r * = pow;
返回r;
}
最难的部分是检查溢出 - 左边作为练习对于
的读者......
For general integer powers, a simple square-and-multiply algorithm is a
good bet, e.g.
unsigned long long power(unsigned a, unsigned b)
{
unsigned long long r, bit, pow;
for(bit=r=1, pow=a; bit<=b; bit<<=1, pow *= pow)
if(b & bit)
r*=pow;
return r;
}
The hardest part is checking for overflow - left as an exercise for the
reader...
这篇关于如何指定数字的力量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!