The number is odd?

Discussion in 'C++' started by nick048, Nov 2, 2006.

  1. nick048

    nick048 Guest

    Hi to all,
    How can verify if a number is odd in C++?

    Thank You and best Regards.
    Gaetano
    nick048, Nov 2, 2006
    #1
    1. Advertising

  2. nick048

    david Guest

    nick048 <> wrote:
    > Hi to all,
    > How can verify if a number is odd in C++?


    If the number is a scalar, then you can do this:

    bool odd = !!(number & 1);

    david
    david, Nov 2, 2006
    #2
    1. Advertising

  3. nick048

    peter koch Guest

    nick048 wrote:
    > Hi to all,
    > How can verify if a number is odd in C++?
    >
    > Thank You and best Regards.
    > Gaetano


    You can do as David suggested - or prefer the less obscure (and at
    least as fast)
    number % 2 != 0

    /Peter
    peter koch, Nov 2, 2006
    #3
  4. nick048

    Geo Guest

    david wrote:
    > nick048 <> wrote:
    > > Hi to all,
    > > How can verify if a number is odd in C++?

    >
    > If the number is a scalar, then you can do this:
    >
    > bool odd = !!(number & 1);
    >
    > david

    Why !! ?
    Geo, Nov 2, 2006
    #4
  5. nick048

    benben Guest

    nick048 wrote:
    > Hi to all,
    > How can verify if a number is odd in C++?
    >
    > Thank You and best Regards.
    > Gaetano
    >


    If an odd number is one that is ot divisible by two then you can just
    divide that number by 2, take the remainder and see if it is 1 or 0;

    Ben
    benben, Nov 2, 2006
    #5
  6. Geo wrote:
    > david wrote:
    > > nick048 <> wrote:
    > > > Hi to all,
    > > > How can verify if a number is odd in C++?

    > >
    > > If the number is a scalar, then you can do this:
    > >
    > > bool odd = !!(number & 1);
    > >
    > > david

    > Why !! ?


    Typo/thinko I expect. Either that or there is some subtle reason not to
    just do this:

    bool odd( number & 1 );


    K
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Nov 2, 2006
    #6
  7. nick048

    david Guest

    Kirit S?lensminde <> wrote:

    > Geo wrote:
    >> david wrote:
    >> > nick048 <> wrote:
    >> > > Hi to all,
    >> > > How can verify if a number is odd in C++?
    >> >
    >> > If the number is a scalar, then you can do this:
    >> >
    >> > bool odd = !!(number & 1);
    >> >
    >> > david

    >> Why !! ?


    (number & 1) is synonym to (number % 2), the remainder of the division by 2.

    I was thinking the logic & operator would be at least as fast as % (modulo).

    The !! is there to convert a scalar to a bool, but it may be unneeded,
    especially in this case where the result is only 0 or 1.

    > Typo/thinko I expect. Either that or there is some subtle reason not to
    > just do this:


    > bool odd( number & 1 );


    This is ok, but your reasons are unclear to me:
    Is this preferred/cleaner/mandatory to initialize a bool ?
    int x = 3; int x(3); aren't they identical ?
    bool is not a basic type ?
    david, Nov 2, 2006
    #7
  8. david <> writes:

    > nick048 <> wrote:
    >> Hi to all,
    >> How can verify if a number is odd in C++?

    >
    > If the number is a scalar, then you can do this:
    >
    > bool odd = !!(number & 1);


    AFAIK not portable. Will fail on systems with ones' complement when
    number is negative (or -0). The valid way is:

    bool odd = number % 2;
    /* note that it can produce, -1, 0 or 1 */

    If you want a value 0 or 1 use either:

    int odd = !!(number % 2);

    or:

    int odd = number % 2 != 0;

    On systems with twos' complement compiler will probably optimize those
    codes to: number & 1.

    --
    Best regards, _ _
    .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
    ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
    ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
    Michal Nazarewicz, Nov 2, 2006
    #8
  9. nick048

    david Guest

    Michal Nazarewicz <> wrote:
    >> bool odd = !!(number & 1);

    > AFAIK not portable. Will fail on systems with ones' complement when
    > number is negative (or -0).


    Right. I was not aware that this could exist.

    david
    david, Nov 2, 2006
    #9
  10. Kirit Sælensminde:

    >> Why !! ?



    !!a

    is short-hand for:

    (bool)a


    > Typo/thinko I expect. Either that or there is some subtle reason not to
    > just do this:
    >
    > bool odd( number & 1 );



    Because it's dressed up to look like you're passing an argument to a
    constructor. Intrinisc types are distinct from user-defined types... don't
    lump them into the same category and come out with bastardisations such as:

    int i(1);

    , it just looks stupid, plus it makes one have to consider the "if it looks
    like a declaration" rule.

    --

    Frederick Gotham
    Frederick Gotham, Nov 2, 2006
    #10
  11. nick048

    Geo Guest

    Frederick Gotham wrote:
    > Kirit Sælensminde:
    >
    > >> Why !! ?

    >
    >
    > !!a
    >
    > is short-hand for:
    >
    > (bool)a
    >

    Is it really?
    Where does it say that ?
    What's the point any way since implicit conversion to bool is going to
    happen for the assignement in the same way that it would happen for the
    '!' !!!.

    > --
    >
    > Frederick Gotham
    Geo, Nov 2, 2006
    #11
  12. Geo:

    >> !!a
    >>
    >> is short-hand for:
    >>
    >> (bool)a
    >>

    > Is it really?
    > Where does it say that ?



    ! converts its value to bool, then inverts it. The second ! will invert it
    again. It has the overall effect of:

    (bool)a


    > What's the point any way since implicit conversion to bool is going to
    > happen for the assignement in the same way that it would happen for the
    > '!' !!!.



    You're right. I've only ever seen it used to suppress compiler warnings.
    The following gives a warning on many compilers:

    bool b = 5 - 3;

    --

    Frederick Gotham
    Frederick Gotham, Nov 2, 2006
    #12
  13. Frederick Gotham:

    > ! converts its value to bool, then inverts it. The second ! will invert it
    > again. It has the overall effect of:
    >
    > (bool)a



    If talking about intrinsic types, of course.

    --

    Frederick Gotham
    Frederick Gotham, Nov 2, 2006
    #13
  14. nick048

    Geo Guest

    Frederick Gotham wrote:
    > Geo:
    >
    > >> !!a
    > >>
    > >> is short-hand for:
    > >>
    > >> (bool)a
    > >>

    > > Is it really?
    > > Where does it say that ?

    >
    >
    > ! converts its value to bool, then inverts it. The second ! will invert it
    > again. It has the overall effect of:
    >
    > (bool)a
    >
    >
    > > What's the point any way since implicit conversion to bool is going to
    > > happen for the assignement in the same way that it would happen for the
    > > '!' !!!.

    >


    I was questioning how it worked, just why it was better/clearer than
    (bool) or static_cast<bool>() ?

    >
    > You're right. I've only ever seen it used to suppress compiler warnings.
    > The following gives a warning on many compilers:
    >
    > bool b = 5 - 3;


    Why would it warn about that but not !(5-3) ?

    >
    > --
    >
    > Frederick Gotham
    Geo, Nov 2, 2006
    #14
  15. Geo wrote:
    > Frederick Gotham wrote:
    >> Geo:
    >>
    >>>> !!a
    >>>>
    >>>> is short-hand for:
    >>>>
    >>>> (bool)a
    >>>>
    >>> Is it really?
    >>> Where does it say that ?

    >>
    >>
    >> ! converts its value to bool, then inverts it. The second ! will
    >> invert it again. It has the overall effect of:
    >>
    >> (bool)a
    >>
    >>
    >>> What's the point any way since implicit conversion to bool is going
    >>> to happen for the assignement in the same way that it would happen
    >>> for the '!' !!!.

    >>

    >
    > I was questioning how it worked, just why it was better/clearer than
    > (bool) or static_cast<bool>() ?


    Because it saves you typing. Because it looks cool. Because when you
    refer to it in a conversation with a colleague you can say "bang-bang-ay"
    instead of "ay-cast-2-bool" or some such. Bang! Bang! Isn't it cool?

    (Oh, that's why it is "better". Clearer? Of course not. But who cares
    when it's so cool?)

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 2, 2006
    #15
  16. nick048

    Geo Guest

    Victor Bazarov wrote:
    > Geo wrote:
    > > Frederick Gotham wrote:
    > >> Geo:
    > >>
    > >>>> !!a
    > >>>>
    > >>>> is short-hand for:
    > >>>>
    > >>>> (bool)a
    > >>>>
    > >>> Is it really?
    > >>> Where does it say that ?
    > >>
    > >>
    > >> ! converts its value to bool, then inverts it. The second ! will
    > >> invert it again. It has the overall effect of:
    > >>
    > >> (bool)a
    > >>
    > >>
    > >>> What's the point any way since implicit conversion to bool is going
    > >>> to happen for the assignement in the same way that it would happen
    > >>> for the '!' !!!.
    > >>

    > >
    > > I was questioning how it worked, just why it was better/clearer than
    > > (bool) or static_cast<bool>() ?

    >
    > Because it saves you typing. Because it looks cool. Because when you
    > refer to it in a conversation with a colleague you can say "bang-bang-ay"
    > instead of "ay-cast-2-bool" or some such. Bang! Bang! Isn't it cool?
    >
    > (Oh, that's why it is "better". Clearer? Of course not. But who cares
    > when it's so cool?)


    I guess, and I suppose I might use it, just for the coolness, but I've
    never met anyone who calls '!' bang.... or '#' pound... but let's not
    go there :)

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    Geo, Nov 2, 2006
    #16
  17. nick048

    Marcus Kwok Guest

    Marcus Kwok, Nov 2, 2006
    #17
  18. Geo wrote:

    >> (Oh, that's why it is "better". Clearer? Of course not. But who cares
    >> when it's so cool?)

    >
    > I guess, and I suppose I might use it, just for the coolness, but I've
    > never met anyone who calls '!' bang.... or '#' pound... but let's not
    > go there :)


    I prefer the Intercal nomenclature for chars. For example " is called rabbit
    ears.

    For those that does not known Intercal, is one of the first languages that
    completely avoided the need for a GO TO instruction. It uses COME FROM
    instead.

    It's a nice language, but many people write coolest code using Standard C++.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Nov 2, 2006
    #18
  19. nick048

    Mark P Guest

    Geo wrote:
    >
    > I guess, and I suppose I might use it, just for the coolness, but I've
    > never met anyone who calls '!' bang.... or '#' pound... but let's not
    > go there :)
    >


    Are you serious? What do you call # if not "pound"? I suppose you
    could call it "sharp", but among people I know (not C# programmers)
    everyone calls it "pound". Is this a geographical thing? (I'm in the U.S.)
    Mark P, Nov 3, 2006
    #19
  20. nick048

    Old Wolf Guest

    david wrote:
    >
    > (number & 1) is synonym to (number % 2), the remainder of the division by 2.


    If the number is negative, then (number & 1) might give
    the wrong result.

    > I was thinking the logic & operator would be at least as fast as % (modulo).


    The compiler will select the fastest operation, you don't
    need to worry. Even the worst compilers will get this right.

    > The !! is there to convert a scalar to a bool, but it may be unneeded,
    > especially in this case where the result is only 0 or 1.


    Other integral types (and pointer types, for that matter), can
    be implicitly converted to bool. You do not need to explicitly
    perform the conversion.

    > bool is not a basic type ?


    Actually it is.
    Old Wolf, Nov 3, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lin
    Replies:
    25
    Views:
    39,116
    taraval
    Feb 4, 2011
  2. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,090
    Richard Heathfield
    Feb 18, 2007
  3. Jay
    Replies:
    0
    Views:
    117
  4. Vikas Maskeri

    odd number of arguments in Nokogiri

    Vikas Maskeri, May 5, 2009, in forum: Ruby
    Replies:
    1
    Views:
    104
    Srijayanth Sridhar
    May 5, 2009
  5. Charlie Ca
    Replies:
    4
    Views:
    113
    Robert Klemme
    Mar 25, 2010
Loading...

Share This Page