Velocity Reviews > C++ > The number is odd?

# The number is odd?

nick048
Guest
Posts: n/a

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

Thank You and best Regards.
Gaetano

david
Guest
Posts: n/a

 11-02-2006
nick048 <(E-Mail Removed)> 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

peter koch
Guest
Posts: n/a

 11-02-2006

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

Geo
Guest
Posts: n/a

 11-02-2006

david wrote:
> nick048 <(E-Mail Removed)> 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 !! ?

benben
Guest
Posts: n/a

 11-02-2006
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

=?iso-8859-1?q?Kirit_S=E6lensminde?=
Guest
Posts: n/a

 11-02-2006

Geo wrote:
> david wrote:
> > nick048 <(E-Mail Removed)> 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

david
Guest
Posts: n/a

 11-02-2006
Kirit S?lensminde <(E-Mail Removed)> wrote:

> Geo wrote:
>> david wrote:
>> > nick048 <(E-Mail Removed)> 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 ?

Michal Nazarewicz
Guest
Posts: n/a

 11-02-2006
david <(E-Mail Removed)> writes:

> nick048 <(E-Mail Removed)> 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--

david
Guest
Posts: n/a

 11-02-2006
Michal Nazarewicz <(E-Mail Removed)> 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

Frederick Gotham
Guest
Posts: n/a

 11-02-2006
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