new type: even or odd

This is a discussion on new type: even or odd within the C++ Programming forums, part of the General Programming Boards category; Hi all, Is there a way to create a new type: even or odd? I'm going to have a int. ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    53

    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

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't understand exactly what you want...how much have you coded thus far?

    Code:
    if (num&1)
      cout<<"odd";
    else
      cout<<"even";
    ??
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Question 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)
    Last edited by DougDbug; 09-04-2003 at 05:35 PM.

  4. #4
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    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?
    Illusion and reality become impartiality and confidence.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Yes it is. It's just more efficient.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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.)

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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));
    }

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    53
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  4. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  5. Changing an objects Type
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2004, 07:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21