-
new type: even or odd
Hi all,
Is there a way to create a new type: even or odd? I'm going to have a int. If the int is even, I'm going to generate data and process it. The same is going to happen for odd int, but I need to keep the even and odd data separate. Is there a way to do this in C# also? Thanks.
JMD
-
I don't understand exactly what you want...how much have you coded thus far?
Code:
if (num&1)
cout<<"odd";
else
cout<<"even";
??
-
typedef
You don't need a new type... unless this is a homework assignment, and you're supposed to do it that way.
I'd recommend a new variable called Even. This variable is non-zero (i.e 1) if the variable you are testing is even. Even is equal to zero if the variable being tested is odd.
Even could be a type bool, or an int.
If you want to create a "new" type, you could create a type called OddEven, which would actually be a type bool.
You can't really create a new type. You can create a new name for one of the valid C++ types. For example, it is common to create a new type ULONG, which is actually an unsigned long.
I usually avoid creating new types, because somebody (maybe me) will have to dig-into some header file somewhere to find-out what that type really is! (Microsoft has no problem with using typedefs... There are tons of 'em in Windows.h)
-
Quote:
Code:
if (num&1)
cout<<"odd";
else
cout<<"even";
This might be a stupid question, but that is an example of bit-trickery roughly equivilent to:
Code:
if (num%2)
cout << "odd";
else
cout << "even";
Do you know somewhere where I could learn more about this?
-
Yes it is. It's just more efficient.
-
ichijoji,
In binary, all even numbers end in zero. That is, bit-zero (the least-significant-bit = LSB) is equal to zero. All odd numbers have bit-zero equal to one.
You can find more info on bitwise operators in the Programming FAQ.
The modulus method is much clearer. Personally, I would avoid the bitwise "trickery", unless efficency (speed) is really important... i.e if you are looping thousands, or millions of times.
( I do use bitwise operators when I'm working with bit-batterns in hardware registers.)
-
I don't think theres a compiler on the planet that will not perform the strength reduction optomizations to turn multiplies divides and modulus into the apropreate shifts and masks. As to my favorate bit twiddle
Code:
inline bool ispow2(unsigned n) {
return n && !(n & (n-1));
}
-
I don't have any code written yet. I've just been thinking about how I'm going to handle all the things that I need to do.
Here's what the program needs to do. It's for a data acquisition systems using a A/D board. I have to take two measurements one measurement is when a magnetic field is increasing (up), and another measurement when the magnetic field is decreasing (down). They're going to be multiple measurements, so the each up measurement will be averaged with the previous up measurement, and the same with the down measurement.
I'm going to keep track of the up and down with a counter. all the up measurements the counter will be odd and down will be even. That way I could determine which measurement is being made.
Should I use array's to keep track of all the data or would another structure be better to use. I'm thinking the most data points in a single array would be 4096 points (double or float).
And then I'm going to do the basic stuff like write the data to a file and have a small graph window to display the data, etc..
I might have to use C#, all the sample programs I have that use the DAQ board are written in C#, anything I should consider if I have to use C#?
Thanks
JMD