am I doing this wrong? I am trying to compare the least significant bit to determine if counter1 is odd.Code:while (counter1 !=0 ,&(counter1&1===>counter1))
Thanks
am I doing this wrong? I am trying to compare the least significant bit to determine if counter1 is odd.Code:while (counter1 !=0 ,&(counter1&1===>counter1))
Thanks
What do you think ",&" and "===>" are supposed to mean?
Um, is that supposed to be a valid snippet of code in C?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'm not sure, though I was told it was a conditional test to see if a number is odd
,& means AND
This could possibly be used to test i counter1 is even or odd: counter1&1
But looking at what you posted... how did you come up with that code? Who told you that "it was a conditional test to see if a number is odd"? Did you randomly add in other characters yourself? What material are you using to learn C?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
it's something someone told me a while back as a simple test to determine if counter1 is odd instead of using a modulo calculation.
Well yes, there's that part that I highlighted. But now, what about the ===> operator? Where have you seen it?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I forgot that AND was && as to the operator I have it saved in my notes I really don't recall where I got it.
Thank you for helping me.
Last edited by Once-ler; 01-02-2014 at 10:05 AM.
Throwing operators/functions that you think are related at a line of code and hoping it works isn't programming. You code can't even compile, because it makes no sense, so it doesn't pass even the first stage of actually DOING anything.
- Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
- A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
- The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.
The definition of an odd integer is "not exactly divisible by 2". Common sense would suggest a test of whether a value is divisible by 2 is sufficient.
And that doing it with a bitwise operation is either unnecessary obfuscation or premature optimisation (or both, when someone writes code they themselves don't understand, as exhibited here).
,&??? you should use correct syntax.
So for a twos complement binary integer where the bits can be considered to be coefficients of a polynomial of powers of 2, then any number with the least significant bit clear must be exactly divisible by 2 since the non-zero coefficients are all multiplied by powers of 2 (so 2 is a common factor for all non-zero terms of such a binary polynomial), and any number with the least significant bit set must be odd.
On any computer where binary numbers are two's complement, then this if statement tests to see if counter is odd:
if(counter&1)
this statement could also be used:
if(0 != (counter&1)) ...
Using modulo should work on any computer, even if it's numbers are not two's complement (which is grumpy's point, and the compiler will probably optimize this to use bit-wise operation if possible):
if(0 != (counter%2))
Last edited by rcgldr; 01-08-2014 at 06:48 AM.
I've written a non-optimized version
Code:#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <math.h> enum { PARITY_ODD, PARITY_EVEN }; int *primefactors(unsigned long long n); int parityeven(long long n); void fatalerr(int err); static int primefactors_r(unsigned n, int **mem, size_t *sz); int main(void) { int n; const char *paritystr[2] = {"odd", "even"}; for (n = -10; n <= 11; n++) printf("%d is %s\n\n", n, parityeven(n)[paritystr]); return 0; } int parityeven(long long n) { int *pfacts; int i, r = PARITY_ODD; if (n < 0) n = -n; if (n == 0 || n == 2) return PARITY_EVEN; if (n == 1 || n == 3) return PARITY_ODD; pfacts = primefactors(n); if (!pfacts) fatalerr(errno); if (pfacts[0] == 0) printf("Is prime"); else printf("Prime Factors:"); for (i = 0; pfacts[i] != 0; i++) { printf(" %d", pfacts[i]); if (pfacts[i] == 2) { r = PARITY_EVEN; } } printf(" => "); return r; } void fatalerr(int err) { fprintf(stdout, "errr... %s\n", strerror(err)); exit(1); } int *primefactors(unsigned long long n) { int *mem; size_t sz = 1; if ((mem = malloc(sizeof *mem * sz)) == NULL) return NULL; if (n < 4) { mem[0] = 0; return mem; } primefactors_r(n, &mem, &sz); mem[sz - 1] = 0; return mem; } static int primefactors_r(unsigned n, int **mem, size_t *sz) { int i, maxn; int *newmem; if (n < 2) return 0; maxn = n / 2; for (i = 2; i <= maxn; i++) { if (n % i == 0) { if (primefactors_r(i, mem, sz)) { (*mem)[*sz - 1] = i; newmem = realloc(*mem, ++(*sz) * sizeof(**mem)); if (!newmem) { int err = errno; free(mem); fatalerr(err); } *mem = newmem; } if (primefactors_r(n / i, mem, sz)) { (*mem)[*sz - 1] = n / i; newmem = realloc(*mem, ++(*sz) * sizeof(**mem)); if (!newmem) { int err = errno; free(mem); fatalerr(err); } } break; } } /* return 0 (false) if no 'lower' primefactors found; * i.e. the above loop didn't terminate early */ return i > maxn; }