|
http://www.codeproject.com/miscctrl/balloonhelp.asp
- NS -
|
|
|
|
|
Hi all,
Could anybody suggest me a simple method to make a number always odd?
For eg.
Suppose a = 14. After conversion it should be 13.
If it is 13 it should be remain as same.
Thank you.
- NS -
|
|
|
|
|
Assume you have got the number in an int:
int oddNumber = 14;
oddNumber |= 1;
or, as in your question (14 should be 13):
int oddNumber = 14;
oddNumber--;
oddNumber |= 1;
|
|
|
|
|
Yes I need the second one.
That's nice. Thank you.
- NS -
|
|
|
|
|
Your'e welcome.
probably it can be done like this (not tested):
int oddNumber = 14;
--oddNumber |= 1;
|
|
|
|
|
Yes it is working...
Thank you again.
- NS -
|
|
|
|
|
You are making a big assumption that the -- operator will execute before the |= operation.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
|
Yes, I have tested it fine.
- NS -
|
|
|
|
|
your code have one failure : negative numbers :
signed char oddNumber = -128;
--oddNumber;
oddNulber |= 1;
|
|
|
|
|
Yes, it's a fact that signed numbers do wrap, eventually.
The original question (and the answer to that specific question) said nothing about negative numbers.
If I remember it right, the question said that 14 should give 13 as output.
There was no rule for handling negative numbers.
Or do I remember it wrong?
If negative numbers doesn't matter, why waste code?
|
|
|
|
|
he didn't talk about negative numbers, but he never exlude them
using int implies that negative values are allowed (otherwise, i think he would have used unsigned ), and odd values can be also either positive or negative...
moreover, writting --oddNumber |= 1; can confuse the reader... that's why i keep militate for the if test solution...
|
|
|
|
|
As long it doesn't confuse the compiler...
|
|
|
|
|
you shouldn't think like that, simply because in your life, not only you won't always be the only one to maintain the code you wrote, but you can also badly understand what you thought beeing obvious when wrote it ; worse when it happens with someone else...
|
|
|
|
|
OK, I agree, such coding requires a good comment line to it, since the function might not be obvious to everyone.
|
|
|
|
|
I know that this code needs good documentation. But I like simple and less processor consuming codes. Further more, in my case there will not be negetive values. So I don't care about the behaviour of this logic with negetive values
- NS -
|
|
|
|
|
if (a%2==0) // number is even
a--; // make it odd
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
U can use like this also.
using conditional operator....
(num%2 == 0)? num : num - 1;
|
|
|
|
|
I think simplest is the one that kakan said. --num |= 1;
- NS -
|
|
|
|
|
|
Maximilien wrote: yeah, but it's unreadable.
Unreadble for whom?
|
|
|
|
|
I swear that there will be a good explanation for this in my cpp!
- NS -
|
|
|
|
|
|
First I thought about this one. But there is constraint in my requirement the new odd number should be less than the actual one if it is even.
So #define makeOdd(x) ((--(x)) |= 1) is required.
- NS -
|
|
|
|
|
I also think that -- =1 is the best solution, from many points of view, from performance to simplicity. That code will easily compile even on small microcontroller implementations without great effort, even those that do not have division capabilities (I've seen % posted on some replies, and that is surely a waste of resources).
I've also seen the question of operator precedence. Actually, that should not be a problem, since, generally speaking, --x means that x will be decremented *before* the value is used. That is the diference between --x and x--.
If anyone has doubts about the precedence then (--(x))|=1 will guarantee adequate precedence, as proposed by NS17. I would sugest to force the precedence to make reading easier, meaning that, in this way, it is not necessary to know the exact meaning of --x by heart, and that the correct evaluation will be explicit instead of implicit.
I would also sugest an improvement that allows for the macro proposed by NS17 to be used even when x is not an l-value. That would be:
#define makeOdd(x) ( ((x)-1) | 1)
This macro could then be used in all cases below:
x=makeOdd(x);
x=makeOdd(14);
z=makeOdd(x)+makeOdd(y);
Rilhas
|
|
|
|