N
nick048
Hi to all,
How can verify if a number is odd in C++?
Thank You and best Regards.
Gaetano
How can verify if a number is odd in C++?
Thank You and best Regards.
Gaetano
nick048 said:Hi to all,
How can verify if a number is odd in C++?
nick048 said:Hi to all,
How can verify if a number is odd in C++?
Thank You and best Regards.
Gaetano
Why !! ?david said:If the number is a scalar, then you can do this:
bool odd = !!(number & 1);
david
nick048 said:Hi to all,
How can verify if a number is odd in C++?
Thank You and best Regards.
Gaetano
Geo said:Why !! ?
Typo/thinko I expect. Either that or there is some subtle reason not to
just do this:
bool odd( number & 1 );
david said:If the number is a scalar, then you can do this:
bool odd = !!(number & 1);
Michal Nazarewicz said:AFAIK not portable. Will fail on systems with ones' complement when
number is negative (or -0).
Typo/thinko I expect. Either that or there is some subtle reason not to
just do this:
bool odd( number & 1 );
Is it really?Frederick said:Kirit Sælensminde:
!!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
'!' !!!.
! converts its value to bool, then inverts it. The second ! will invert it
again. It has the overall effect of:
(bool)a
Frederick said:Geo:
! converts its value to bool, then inverts it. The second ! will invert it
again. It has the overall effect of:
(bool)a
(bool) or static_cast said: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;
Geo said:I was questioning how it worked, just why it was better/clearer than
(bool) or static_cast<bool>() ?
Victor said: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?)
Geo said:never met anyone who calls [...] '#' pound... but let's not
go there
Geo said: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
Geo said: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
david said:(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.
bool is not a basic type ?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.