mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2008-09-10, 17:48   #12
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

22·1,549 Posts
Default

Quote:
Originally Posted by nuggetprime View Post
The above was not stated precisely, I think.
I want to know if in C, when i define a ,say, unsigned short int, how can I access a certain bit of it,in reasonable CPU time?
Thanks,
--nuggetprime
I would have thought a simple bitwise and mask would suffice. (1 << bit_num) && the_int
retina is online now   Reply With Quote
Old 2008-09-10, 17:56   #13
jrk
 
jrk's Avatar
 
May 2008

3·5·73 Posts
Default

You can use the bitwise operator 'and' (&) and a bitmask to test a specific bit.

x & 0x0001
x & 0x0002
x & 0x0004
x & 0x0008
x & 0x0010
etc

Or if you want to use a variable to specify the bit, you can use a shift operator (>>).

x >> y & 0x0001
jrk is offline   Reply With Quote
Old 2008-09-10, 17:58   #14
nuggetprime
 
nuggetprime's Avatar
 
Mar 2007
Austria

2×151 Posts
Default

It works. Thanks a lot retina & jrk.
If you read this, Xyzzy,can you please rename this thread to "Questions on coding my FFT&LLR implementation"?

Thanks,
--nuggetprime

Last fiddled with by nuggetprime on 2008-09-10 at 17:59
nuggetprime is offline   Reply With Quote
Old 2008-09-13, 08:32   #15
nuggetprime
 
nuggetprime's Avatar
 
Mar 2007
Austria

2×151 Posts
Question Very strange problem

If I compile the following C code and run the program, it doesn't print anything!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
unsigned short apl_uiswap(unsigned short nm)
{
unsigned short buf;
int le;
for (le=0;le>16;le++)
{
printf("HI\n");
}
return 3;
}
int main(int argc, char* argv[])
{
apl_uiswap(43);
}
Is that the same for your system?
What the hell is going on here??
nuggetprime is offline   Reply With Quote
Old 2008-09-13, 08:37   #16
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

22·1,549 Posts
Default

One presumes you mean: for (le=0;le<16;le++)
retina is online now   Reply With Quote
Old 2008-09-13, 08:44   #17
nuggetprime
 
nuggetprime's Avatar
 
Mar 2007
Austria

4568 Posts
Default

Thanks retina.
well, it now prints
But if I print le before the loop, it always prints a somewhat random integer:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
unsigned short apl_uiswap(unsigned short nm)
{
unsigned short buf;
unsigned short le;
printf("%d\n",le);
for (le=0;le<16;le++)
{
printf("HI\n");
}
return 3;
}
int main(int argc, char* argv[])
{
apl_uiswap(43);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
unsigned short apl_uiswap(unsigned short nm)
{
unsigned short buf;
unsigned short le;
printf("%d\n",le);
for (le=0;le<16;le++)
{
printf("HI\n");
}
return 3;
}
int main(int argc, char* argv[])
{
apl_uiswap(43);
}
is that normal?

thanks,
nuggetprime
nuggetprime is offline   Reply With Quote
Old 2008-09-13, 08:49   #18
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

183416 Posts
Default

Quote:
Originally Posted by nuggetprime View Post
But if I print le before the loop, it always prints a somewhat random integer:
is that normal?
Sure it is. You're printing the value of an uninitialised variable, it could be anything. Not until the "le=0" is encountered does le become a known value.

Last fiddled with by retina on 2008-09-13 at 08:50
retina is online now   Reply With Quote
Old 2008-09-13, 09:51   #19
nuggetprime
 
nuggetprime's Avatar
 
Mar 2007
Austria

2·151 Posts
Default

If I write
Code:
void aplint_init(apl_int pt)
{
pt.dg=(unsigned short*) calloc(1,2);
}
i get a segfault.
but if I write
Code:
#define aplint_init(x) x.dg=(unsigned short *)calloc(1,2)
everything is ok.

What am i doing wrong?

thanks,
nugget

Last fiddled with by nuggetprime on 2008-09-13 at 09:51
nuggetprime is offline   Reply With Quote
Old 2008-09-13, 16:30   #20
nuggetprime
 
nuggetprime's Avatar
 
Mar 2007
Austria

2×151 Posts
Default

Solved above thing.
The next thing I think i'm not sure about is speed related.If you have a large set of numbers and have to add them,
is it then faster or equally fast to directly compute x1+x2+x3... instead of adding a2 to a1, then adding a3 to the result and so on...

Nice day,
nuggetprime

Last fiddled with by nuggetprime on 2008-09-13 at 16:30
nuggetprime is offline   Reply With Quote
Old 2008-09-13, 16:41   #21
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

22×1,549 Posts
Default

Quote:
Originally Posted by nuggetprime View Post
The next thing I think i'm not sure about is speed related.If you have a large set of numbers and have to add them,
is it then faster or equally fast to directly compute x1+x2+x3... instead of adding a2 to a1, then adding a3 to the result and so on...
Short answer: It depends upon many many factors.

Long answer: Just code up the two methods you propose and see which is faster on your hardware with your data with your set sizes.
retina is online now   Reply With Quote
Old 2008-09-13, 18:05   #22
nuggetprime
 
nuggetprime's Avatar
 
Mar 2007
Austria

2×151 Posts
Default

Thanks again retina.
One question more, if I may do some more "spamming" here:
I'm not able to get long double precision. If I run:
Code:
#include <stdio.h>
int main()
{
long double c;
c=1/7.0;
printf("%3.30Lf\n",c);
return 0;
}
it prints:
Code:
0.142857142857142849212692681249
As seen, only the first 16 digits of the mantissa are true, which is equal to normal double. What's wrong?

Thanks,
nuggetprime
nuggetprime is offline   Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Anyone know enough about coding to do this? jrafanelli Software 2 2018-01-11 15:16
Python Coding Help? kelzo Programming 3 2016-11-27 05:16
Zhang's OPQBT coding help? flouran Programming 0 2009-07-25 02:43
coding midlet for TF starrynte Programming 1 2008-12-30 22:31
Coding Challenges R.D. Silverman Programming 18 2005-08-09 13:14

All times are UTC. The time now is 20:32.


Fri Jul 16 20:32:04 UTC 2021 up 49 days, 18:19, 1 user, load averages: 1.65, 1.93, 2.05

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.