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+a toi(textip[1])*256*256+atoi( textip[2])*256+atoi(text ip[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. 22 1711
Yanb <Ya**@whatever. invalidwrites:
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.
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+a toi(textip[1])*256*256+atoi( textip[2])*256+atoi(text ip[3]);
When I wrote *(256^3), I got fake results. I'd like to use something
more sophisticated, instead of 256*256
^ 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...
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+a toi(textip[1])*256*256+atoi( textip[2])*256+atoi(text ip[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
On 18 Apr 2008 at 11:01, Bartc wrote:
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.
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...
Ben Bacarisse <be********@bsb .me.ukwrote in
news:87******** ****@bsb.me.uk:
Yanb <Ya**@whatever. invalidwrites:
>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.
Hi, and thank you for reply. I have read some very basics, but usually I need a language for a specific
thing and as soon as I know enough to complete the task, I leave learning till later :-/
Do you have some tip for a newbie where to start? Of course I can google out load of pages about c,
but no search engine can compete with reference from a human ;-)
>
>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+a toi(textip[1])*256*256+atoi( tex tip[2])*256+atoi(text ip[3]);
When I wrote *(256^3), I got fake results. I'd like to use something more sophisticated, instead of 256*256
^ 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.
Oh yes, that's it, thanks, shifting I know shifts from php ;-) This way will need less cpu cycles than
multiplication.
Ok, so there no way to write mathematic "power of" in c?
Yanb <Ya**@whatever. invalidwrote:
Ben Bacarisse <be********@bsb .me.ukwrote in
news:87******** ****@bsb.me.uk:
^ 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.
Oh yes, that's it, thanks, shifting I know shifts from php ;-) This way
will need less cpu cycles than multiplication.
I wouldn't bet on that, a good compiler probably will "see" if
a multiplication can be replaced by a shift operation and will
use that if it's faster on the machine you're compiling for.
Ok, so there no way to write mathematic "power of" in c?
While there's no "power of" operator like for example in FORTRAN
there's the pow() function from the math part of the standard
C library, but which takes double arguments and returns a double.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Antoninus Twink <no****@nospam. invalidwrote in
news:sl******** ***********@nos pam.invalid:
On 18 Apr 2008 at 11:01, Bartc wrote:
>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.
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...
Thank you both. I must admit, that the code Is almost a mystery for me :-) jt@toerring.de (Jens Thoms Toerring) wrote in news:66******** *****@mid.uni-berlin.de:
[snip]
Thank you for a good advices, Jens. Good to know it.
On 18 Apr 2008 at 21:46, user923005 wrote:
If we look at his original post (and knowing he is just turning these
TCP/IP addresses into 4 byte integers):
"
numericip=atoi( textip[0])*256*256*256+a toi(textip[1])*256*256+atoi( textip[2Â*])*256+atoi(text ip[3]);"
It could clearly be done as {assuming textip[] is an array of unsigned
char} as:
numericip = (unsigned long)textip[0] << 24 +
(unsigned long)textip[1] << 16 +
(unsigned long)textip[2] << 8 +
(unsigned long)textip[3] ;
True, and someone had already suggested this. But the discussion
broadened, and someone claimed that using pow() was the best way to
raise integers to (positive) integer powers: it was this assertion that
I was responding to.
Or {better yet} simply use inet_aton() {which is also fine for
Winsock, though WSAStringToAddr ess() is an alternative}. That is what
would be normally done to retrieve a TCP/IP address from a dotted
address string.
HOWEVER!
The OP should keep in mind that inet_aton() does NOT support IPv6 and
that getnameinfo() should be used instead for IPv4/v6 dual stack
support.
Good advice.
To the O.P.:
You can't go wrong with "Unix Network Programming" By W. Richard
Stevens
Agreed. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Zero |
last post by:
Hi,
I am calculating an integer to the pwer of a large integer, e.g.
2^5000.
It turns out that the result I always get is zero. I am sure that the
result is too large to store in such type as u_int64_t and long int
etc.
My power function is as follows: for(i=0; i<5000; i++) result *= 2;
|
by: Matt |
last post by:
Give a one-line C expression to test whether a number is a power of 2.
No loops allowed.
|
by: MJ |
last post by:
Hi
I have question about the C.
I have a number X which is power of two
X = 2 power n
I need to find it using a Single C statement whether its a power of 2
or not
I know that a number with power of 2 will have only one single bit as 1
and rest of the bit as 0. To check the bit I need to go through all the
bits ones and check for the condition. I dont want use a loop. In that
case how can I do this
|
by: JD |
last post by:
Hi guys
What's the best way to specify font size using CSS? I try to avoid
absolute units like pt and px because then users can't resize the fonts
in IE, but % and em are a complete pain to use IMO. I read somewhere (a
W3C tip I think) that the best way is to specify a "base" size and then
have all your fonts relative that, but I'm not sure how that works.
TIA
|
by: Steve Kershaw |
last post by:
This is stupid!!!!
I have looked everywhere (in books, google, online....) to find out how
to raise a number to a power and I can't find it!!!
For example:
given x = 10
y = x ^ 2;
y would then be 100 (10 squared is 100).
| |
by: raghu |
last post by:
is it possible to write a code using bitwise operators for checking
whether a number is power of 2 or in general power of n
(n=2,3,4,...)??
Thanks a lot.
Regards,
Raghu
|
by: David T. Ashley |
last post by:
What is the most economical test in 'C' for "integer is a power of 2"?
For example, something better than:
void is_2_pow(int arg)
{
return((x == 1) || (x == 2) || (x == 4) || (x == 8) || (x == 16) /* and
so on */ );
}
|
by: maxthebaz |
last post by:
Our machines have this requirement: if power failure occurs, many
important variables are to be resumed from where they were interrupted
after the machine is restarted (power on in this case). In other words,
the basic idea is to keep a snapshot of the state machine before it is
interrupted.
The board is provided with:
- a 32-bit H8S/2633 Hitachi microprocessor;
- a battery-backed memory (BBM), where these variables are stored; BBM
area...
|
by: calimero22 |
last post by:
Hi
I'musing the GMP Libraries Mathematics.
I need to calculate 12345678987654321.1234567876554456 ^
9876543212344556676.676767676
How can i do ?
The two variables are type MPF.
Thanks
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |