Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Bitwise Not Operator |
| Date | 2012-06-26 15:17 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4FEA0AE7.2020308@verizon.net> (permalink) |
| References | <5baf9e7b-f46f-4aad-9e44-aa4c12cb0041@googlegroups.com> <slrn3vfsjujqvp.jur.ike@iceland.freeshell.org> <12aac795-58c8-4810-b017-4034c655bf50@googlegroups.com> |
On 06/26/2012 02:40 PM, aleksa wrote: > On Tuesday, June 26, 2012 7:06:01 PM UTC+2, Ike Naar wrote: >> On 2012-06-26, aleksa <aleksazr@gmail.com> wrote: >>> unsigned char X, Y; >>> if (X != (unsigned char) ~Y) >>> >>> why do I get a warning here? >>> comparison of promoted ~unsigned with unsigned >> >> This is unexpected. >> Looking a the format of the warning, it seems you're using gcc. >> But gcc 4.1.2 only gives this warning when the cast to unsigned char >> is missing, as in (X != ~Y). If the cast is in place, the compiler >> remains silent. Which version of gcc do you use? > > 4.6.2. > > Actually, the original warning message is ended with [-Wsign-compare] > don't know why I haven't included that part... > (that option is included with -Wextra). > > -Wsign-compare: > "Warn when a comparison between signed and unsigned values..." > but, aren't both values here unsigned? Both values start out as unsigned, but if UCHAR_MAX <= INT_MAX, both X and (unsigned char)~Y get promoted to int before the comparison is performed. However, regardless of whether it's looking at the original types or the promoted types, and regardless of whether UCHAR_MAX > INT_MAX, the signedness is the same on both sides of the expression, so I don't understand why the compiler would generate that particular warning. It's the ~Y expression that needs warning about. It's generally a bad idea to perform bitwise operations on signed types, and due to the integer promotions, that's precisely what ~Y does (unless UCHAR_MAX > INT_MAX). What you probably want is something like: X != UCHAR_MAX & ~(unsigned)Y
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Bitwise Not Operator aleksa <aleksazr@gmail.com> - 2012-06-26 08:35 -0700
Re: Bitwise Not Operator Ike Naar <ike@iceland.freeshell.org> - 2012-06-26 17:06 +0000
Re: Bitwise Not Operator aleksa <aleksazr@gmail.com> - 2012-06-26 11:40 -0700
Re: Bitwise Not Operator Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-26 20:57 +0200
Re: Bitwise Not Operator James Kuyper <jameskuyper@verizon.net> - 2012-06-26 15:17 -0400
Re: Bitwise Not Operator Les Cargill <lcargill99@comcast.com> - 2012-06-26 12:31 -0500
csiph-web