Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13239
| From | yatremblay@bel1lin202.(none) (Yannick Tremblay) |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Coding Style Question - Is there any sane reason for doing this? |
| Date | 2012-01-11 10:37 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <jejops$ff5$2@dont-email.me> (permalink) |
| References | <40a2328f-2cd8-4dc6-a0e5-4894187042ce@q8g2000yqa.googlegroups.com> <Xns9FD65BD51F41Amyfirstnameosapriee@216.196.109.131> <jegoav$5te$1@speranza.aioe.org> <jejnve$ff5$1@dont-email.me> |
In article <jejnve$ff5$1@dont-email.me>, none) (Yannick Tremblay <yatremblay@bel1lin202.> wrote: >In article <jegoav$5te$1@speranza.aioe.org>, BCFD36 <bcfd36@cruzio.com> wrote: >>On 1/9/12 11:01 PM, Paavo Helde wrote: >>> BCFD36<bcfd36@cruzio.com> wrote in news:jegihu$ri7$1@speranza.aioe.org: >>> >>>> On 1/9/12 5:30 PM, Ian Collins wrote: >>>>> On 01/10/12 02:11 PM, BCFD36 wrote: >>>>>> I am working on a piece of legacy code that has many, shall we say, >>>>>> strange things. This one, as J.K. Anderson (classics Prof at UC >>>>>> Berkeley) used to say, "baffles me". It has the following statement >>> in >>>>>> the code: >>>>>> >>>>>> const unsigned int WAKEUP_MASK = (1<< 1) ; >>>>>> >>>>>> Is there any reason for doing this, other than to make the code even >>>>>> more obscure? >>>>> >>>>> In addition to Alf's comments, the form is quite common especially in >>>>> the embedded or driver world. Although it is more common to see >>>>> something like >>>>> >>>>> const unsigned wakeupBit = 1; >>>>> const unsigned someOtherBit = 2; >>>>> >>>>> // other bit definitions >>>>> >>>>> const uint32_t wakeupMask = (1u<< wakeupBit); >>>>> >>>> >>>> Interesting. I didn't know the form is common. I've never seen it >>>> before, but I don't do much driver work and haven't seen it in the >>>> embedded work I have done. >>>> >>>> Personally, I find it quite obscure and prefer the 0x0002 notation >>>> myself. But I will have to think on it some more. I can see how you >>>> could OR together a bunch of these and it be pretty clear which bits >>> are >>>> set. But I still find it obscure. >>> >>> The "1<<..." style is better for several reasons, most of which kick in >>> when you have larger values than just 0x2: >>> >>> - it clearly documents this is a bit mask >>> - it clearly documents there is only one bit set >>> - it clearly documents which bit it is so one can easily avoid >>> collisions with other masks and see which bits are unused >>> >>> The hexadecimal notation can also do all these things, but requires more >>> mental effort from the reader and familiarity with the hexadecimal >>> notation in the first place. >> >>Only two small quibbles... it was not at all clear to me what this was >>supposed to indicate. Now that I know the secret handshake, it makes >>more sense. And doing it in HEX is no problem at all, at least for me. >>Maybe it is the dinosaur in me. > >0x0002 or 0x0008 is rather trivial and does not cause me to stop. >However, when the bitmask becomes used for multiple things, then I >would say that the following reads trivially while a pure hex based >implementation would force me to stop and start to very carefully >count bits: > >const unsigned wakeupBit = 1; >const unsigned someOtherBit = 2; >... >const unsigned superBit = 13; >... >const unsigned hyperBit = 23; >const unsigned gigaBit = 24; >... >const unsigned ultraBit = 29; Arggh!!! Obviously | not & >const uint32_t theMask = (1u<< wakeupBit) > | (1u<< someOtherBit) > | (1u<< superBit) > | (1u<< hyperBit) > | (1u<< gigaBit) > | (1u<< ultraBit) ; > >followed elsewhere by: > >if( theMask & superBit ) ...
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Coding Style Question - Is there any sane reason for doing this? BCFD36 <bcfd36@aol.com> - 2012-01-09 17:11 -0800
Re: Coding Style Question - Is there any sane reason for doing this? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-10 02:23 +0100
Re: Coding Style Question - Is there any sane reason for doing this? legalize+jeeves@mail.xmission.com (Richard) - 2012-01-10 06:05 +0000
Re: Coding Style Question - Is there any sane reason for doing this? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-10 07:12 +0100
Re: Coding Style Question - Is there any sane reason for doing this? BCFD36 <bcfd36@cruzio.com> - 2012-01-09 22:35 -0800
Re: Coding Style Question - Is there any sane reason for doing this? Gareth Owen <gwowen@gmail.com> - 2012-01-10 06:58 +0000
Re: Coding Style Question - Is there any sane reason for doing this? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-10 21:58 +0000
Re: Coding Style Question - Is there any sane reason for doing this? David Dyer-Bennet <dd-b@dd-b.net> - 2012-01-10 16:46 -0600
Re: Coding Style Question - Is there any sane reason for doing this? jt@toerring.de (Jens Thoms Toerring) - 2012-01-10 23:41 +0000
Re: Coding Style Question - Is there any sane reason for doing this? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-11 00:19 +0000
Re: Coding Style Question - Is there any sane reason for doing this? "MikeWhy" <boat042-nospam@yahoo.com> - 2012-01-10 21:53 -0600
Re: Coding Style Question - Is there any sane reason for doing this? Juha Nieminen <nospam@thanks.invalid> - 2012-01-11 15:14 +0000
Re: Coding Style Question - Is there any sane reason for doing this? Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-11 11:46 -0500
Re: Coding Style Question - Is there any sane reason for doing this? Ian Collins <ian-news@hotmail.com> - 2012-01-10 14:30 +1300
Re: Coding Style Question - Is there any sane reason for doing this? BCFD36 <bcfd36@cruzio.com> - 2012-01-09 21:32 -0800
Re: Coding Style Question - Is there any sane reason for doing this? Paavo Helde <myfirstname@osa.pri.ee> - 2012-01-10 01:01 -0600
Re: Coding Style Question - Is there any sane reason for doing this? BCFD36 <bcfd36@cruzio.com> - 2012-01-09 23:11 -0800
Re: Coding Style Question - Is there any sane reason for doing this? yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-01-11 10:23 +0000
Re: Coding Style Question - Is there any sane reason for doing this? yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-01-11 10:37 +0000
Re: Coding Style Question - Is there any sane reason for doing this? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-10 00:30 -0800
Re: Coding Style Question - Is there any sane reason for doing this? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-10 16:31 +0100
Re: Coding Style Question - Is there any sane reason for doing this? scott@slp53.sl.home (Scott Lurndal) - 2012-01-10 17:14 +0000
Re: Coding Style Question - Is there any sane reason for doing this? Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-10 15:11 -0500
Re: Coding Style Question - Is there any sane reason for doing this? Richard <rgrdev_@gmail.com> - 2012-01-10 21:45 +0100
Re: Coding Style Question - Is there any sane reason for doing this? hanukas <jukka@liimatta.org> - 2012-01-11 00:04 -0800
Re: Coding Style Question - Is there any sane reason for doing this? hanukas <jukka@liimatta.org> - 2012-01-11 00:15 -0800
Re: Coding Style Question - Is there any sane reason for doing this? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-10 21:51 +0000
Re: Coding Style Question - Is there any sane reason for doing this? "Bill Davy" <Bill@SynectixLtd.com> - 2012-01-11 08:30 +0000
Re: Coding Style Question - Is there any sane reason for doing this? "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2012-01-11 09:40 +0100
Re: Coding Style Question - Is there any sane reason for doing this? hanukas <jukka@liimatta.org> - 2012-01-11 06:50 -0800
Re: Coding Style Question - Is there any sane reason for doing this? scott@slp53.sl.home (Scott Lurndal) - 2012-01-11 17:28 +0000
Re: Coding Style Question - Is there any sane reason for doing this? David Dyer-Bennet <dd-b@dd-b.net> - 2012-01-11 13:28 -0600
Re: Coding Style Question - Is there any sane reason for doing this? jgk@panix.com (Joe keane) - 2012-01-11 17:51 +0000
Re: Coding Style Question - Is there any sane reason for doing this? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-12 05:40 -0800
Re: Coding Style Question - Is there any sane reason for doing this? BCFD36 <bcfd36@cruzio.com> - 2012-01-12 20:42 -0800
Re: Coding Style Question - Is there any sane reason for doing this? hanukas <jukka@liimatta.org> - 2012-01-10 23:50 -0800
csiph-web