473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

The number is odd?

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

Thank You and best Regards.
Gaetano

Nov 2 '06 #1
28 12124
nick048 <ni*************@moonsoft.itwrote:
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
Nov 2 '06 #2

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

Nov 2 '06 #3
Geo

david wrote:
nick048 <ni*************@moonsoft.itwrote:
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 !! ?

Nov 2 '06 #4
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
Nov 2 '06 #5

Geo wrote:
david wrote:
nick048 <ni*************@moonsoft.itwrote:
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

Nov 2 '06 #6
Kirit S?lensminde <ki****************@gmail.comwrote:
Geo wrote:
>david wrote:
nick048 <ni*************@moonsoft.itwrote:
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 ?
Nov 2 '06 #7
david <de**@free.frwrites:
nick048 <ni*************@moonsoft.itwrote:
>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--
Nov 2 '06 #8
Michal Nazarewicz <mi****@tlen.plwrote:
> 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
Nov 2 '06 #9
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
Nov 2 '06 #10
Geo

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
Nov 2 '06 #11
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
Nov 2 '06 #12
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
Nov 2 '06 #13
Geo

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
Nov 2 '06 #14
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
Nov 2 '06 #15
Geo

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
Nov 2 '06 #16
Geo <gg@remm.orgwrote:
never met anyone who calls [...] '#' pound... but let's not
go there :)
Actually, we already did :)
http://groups.google.com/group/comp....092e9d325aa4f/

and even before I started reading this group:
http://groups.google.com/group/comp....7c712f09983b7/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 2 '06 #17
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
Nov 2 '06 #18
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.)
Nov 3 '06 #19
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.

Nov 3 '06 #20
Mark P wrote:
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.)
Everyone I know calls it "hash". This is particularly
appropriate for describing the language "C hash" too.

Nov 3 '06 #21
Mark P wrote:
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.)
You obviously haven't used a UK keyboard, where shift3 is the £ (GBP)
symbol. # is hash.

--
Ian Collins.
Nov 3 '06 #22
Old Wolf wrote:
Mark P wrote:
>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.)

Everyone I know calls it "hash". This is particularly
appropriate for describing the language "C hash" too.
But what about the language C tic-tac-toe-board?

Where in the world are you located?
Nov 3 '06 #23
Mark P wrote:
Old Wolf wrote:
>Mark P wrote:
>>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.)

Everyone I know calls it "hash". This is particularly
appropriate for describing the language "C hash" too.

But what about the language C tic-tac-toe-board?

Where in the world are you located?
It's a view on the world from a gaol cell. The language is
"See, gaoled!"

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 3 '06 #24

david wrote:
Kirit S?lensminde <ki****************@gmail.comwrote:
Geo wrote:
david wrote:
nick048 <ni*************@moonsoft.itwrote:
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 ?
They are identical. It was the lack of the double '!' that I was
referring to.

bool odd = ( number & 1 );
K

Nov 3 '06 #25

Frederick Gotham wrote:
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.
I'm not sure that I agree with you on that. The important distinction
between the types we use is whether they follow object or value
semantics, not whether they happen to be in the sub-set of types that
happen to be defined by keywords in the standard.

std::string s( "Hello" );

Presumably you consider the above OK? It invites equal consideration to
the "if it looks like a declaration" rule.
K

Nov 3 '06 #26
On Thu, 2 Nov 2006 10:43:57 +0000 (UTC), david <de**@free.frwrote in
comp.lang.c++:
Kirit S?lensminde <ki****************@gmail.comwrote:
Geo wrote:
david wrote:
nick048 <ni*************@moonsoft.itwrote:
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.
No, it is not. Not for integer types with negative values on 1's
complement systems, which the C++ language specifically supports.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 3 '06 #27
>>"Geo" <gg@remm.orgwrites:
>>>Why !! ?
Frederick Gotham wrote:
>>
!!a

is short-hand for:

(bool)a
"Geo" <gg@remm.orgwrites:
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
'!' !!!.
In this particular reason it will but consider a code:

#v+
int arr[10] = { /* some initial values */ };
int num = 0;
for (int i = 0; i<10; ++i) num += arr[i] % 2;
#v-

counting how many odd numbers there are in arr. It'll fail since
arr[i]%2 may return -1 so here you have to cast it, use !! or ?: and
!! is the shortest form.

--
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--
Nov 3 '06 #28
Kirit Sælensminde:
std::string s( "Hello" );

Presumably you consider the above OK?

I use the form for two reasons:

(1) It looks like a function call/constructor call.
(2) It doesn't require a copy-construct as does the other form.

--

Frederick Gotham
Nov 3 '06 #29

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Shay Hurley | last post by:
this is probably a stupid question so apologies in advance. I am trying to format a number to look like a phone number with "-"'s between the numbers etc e.g. 15554256987 should be formatted as...
8
by: EAS | last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around here a lot... Anyways, I've found out how to make a "guess my number game" where the player guesses a number between...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
27
by: Gaijinco | last post by:
Sooner or later everytime I found recreational programming challenges I stumble with how I test if a number is has decimal places differnt than 0? For example if I want to know if a number is a...
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
5
by: silversnake | last post by:
I'm trying to write a program that take a input number and prints if is a prime numbers but is not working for instance, it says that 4 is prime while 5 is not. can anyone see what the problem is ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.