446,194 Members | 817 Online
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

# The number is odd?

 P: n/a Hi to all, How can verify if a number is odd in C++? Thank You and best Regards. Gaetano Nov 2 '06 #1
28 Replies

 P: n/a nick048

 P: n/a 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

 P: n/a david wrote: nick048

 P: n/a 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

 P: n/a Geo wrote: david wrote: nick048

 P: n/a Kirit S?lensminde david wrote: nick048

 P: n/a david 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 +-------ooO--(_)--Ooo-- Nov 2 '06 #8

 P: n/a Michal Nazarewicz 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

 P: n/a 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

 P: n/a 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

 P: n/a Geo: > !!ais 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

 P: n/a 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

 P: n/a 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() ? > 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

 P: n/a Geo wrote: Frederick Gotham wrote: >Geo: >>> !!ais short-hand for: (bool)aIs it really?Where does it say that ? ! converts its value to bool, then inverts it. The second ! willinvert it again. It has the overall effect of: (bool)a >>What's the point any way since implicit conversion to bool is goingto happen for the assignement in the same way that it would happenfor the '!' !!!. I was questioning how it worked, just why it was better/clearer than (bool) or static_cast() ? 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

 P: n/a Victor Bazarov wrote: Geo wrote: Frederick Gotham wrote: Geo: !!ais short-hand for: (bool)aIs 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 goingto happen for the assignement in the same way that it would happenfor the '!' !!!. I was questioning how it worked, just why it was better/clearer than (bool) or static_cast() ? 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

 P: n/a Geo

 P: n/a Geo wrote: >(Oh, that's why it is "better". Clearer? Of course not. But who careswhen 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a Mark P wrote: Geo wrote: >>I guess, and I suppose I might use it, just for the coolness, but I'venever met anyone who calls '!' bang.... or '#' pound... but let's notgo 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

 P: n/a Old Wolf wrote: Mark P wrote: >Geo wrote: >>I guess, and I suppose I might use it, just for the coolness, but I'venever met anyone who calls '!' bang.... or '#' pound... but let's notgo there :) Are you serious? What do you call # if not "pound"? I suppose youcould 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

 P: n/a Mark P wrote: Old Wolf wrote: >Mark P wrote: >>Geo wrote:I guess, and I suppose I might use it, just for the coolness, butI've never met anyone who calls '!' bang.... or '#' pound... butlet's not go there :)Are you serious? What do you call # if not "pound"? I suppose youcould call it "sharp", but among people I know (not C# programmers)everyone calls it "pound". Is this a geographical thing? (I'm inthe U.S.) Everyone I know calls it "hash". This is particularlyappropriate 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

 P: n/a david wrote: Kirit S?lensminde

 P: n/a 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

 P: n/a On Thu, 2 Nov 2006 10:43:57 +0000 (UTC), david

 P: n/a >>"Geo" >>Why !! ? Frederick Gotham wrote: >> !!ais short-hand for: (bool)a "Geo" -----ooO--(_)--Ooo-- Nov 3 '06 #28

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.