Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24189
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2014-05-16 11:21 -0700 |
| References | (4 earlier) <537521ca$0$6665$9b4e6d93@newsspool3.arcor-online.net> <XnsA32F79A3CBC3eejj99@194.109.133.133> <53754d1f$0$6666$9b4e6d93@newsspool3.arcor-online.net> <XnsA32F623E24696eejj99@194.109.133.133> <5375ed5d$0$6710$9b4e6d93@newsspool2.arcor-online.net> |
| Message-ID | <79376114-e0fc-47de-bebe-8cba36e313cf@googlegroups.com> (permalink) |
| Subject | Re: bitwise vs. logical OR operator (was: ISO 8601 Code Review Wanted) |
| From | dhtml <dhtmlkitchen@gmail.com> |
On Friday, May 16, 2014 3:50:15 AM UTC-7, Christoph Michael Becker wrote:
> Evertjan. wrote:
>
>
>
> > Christoph Michael Becker <cmbecker69@arcor.de> wrote on 16 mei 2014 in
>
> > comp.lang.javascript:
>
> >
>
> >> Evertjan. wrote:
>
> >>
>
> >>> Christoph Michael Becker <cmbecker69@arcor.de> wrote on 15 mei 2014 in
>
> >>> comp.lang.javascript:
>
> >>>
>
> >>>> Furthermore there seems to be a bug at the end of setTime, where you're
>
> >>>> using a | operator instead of || for the default of ss.
>
> >>>
>
> >>> alert(12|0) // 12
>
> >>> alert(12|1) // 13 !
>
> >>> alert(13|1) // 13
>
> >>> alert('13'|1) // 13
>
> >>> alert('13'|0) // 13
>
> >>> alert('13x'|0) // 0 !
>
> >>>
>
> >>> alert(12||0) // 12
>
> >>> alert(12||1) // 12 !
>
> >>> alert(13||1) // 13
>
> >>> alert('13'||1) // 13
>
> >>> alert('13'||0) // 13
>
> >>> alert('13x'||0) // 13x !
>
> >>>
>
> >>> methinks ss|0 is slightly better,
>
> >>> as it zeros inconvertable strings.
>
> >>
>
> >> No, at least not in this case.
>
> >
>
> > I was not talking about this special case.
>
>
>
> Okay. It wasn't clear to me because you had not changed the subject
>
> line, and have been quoting the issue in the OP's code that I mentioned.
>
>
>
> >> ss is either a string with exactly two
>
> >> decimal digits or undefined (see the OP's full code[1]).
>
> >
>
> > In general one would consider all cases.
>
>
>
> Not necessarily. It depends on what one wants to achieve.
>
>
>
> >> As you already
>
> >> have examined, using the binary bitwise OR operator can lead to false
>
> >> results,
>
> >
>
> > Not in the case of x|0, as it does not change any numeric x,
>
> > and zeros all other x values.
>
>
>
> My bad. Of course you're right wrt. to values that can be represented
>
> as 32 bit integers.
>
>
Right.
var x = Math.pow(2, 31);
x; // 2147483648
x|0; // -2147483648
>
> > Now x||0 does not do that in the case that x is a string.
>
> >
>
> >> while the binary logical OR operator is exactly what is
>
> >> desired: use zero as default, when ss is falsy.
>
> >
>
> > This NG is about javascript, I wish to discuss general cases.
>
>
>
> However, whether the bitwise or the logical OR operator is correct (or
>
> better suited) depends on the requirements. These requirements have to
>
> be stated, before a solution can be discussed. It is not clear to me
>
> which requirements you have in mind when suggesting using |0.
>
> Converting a numeric argument to a number can also be done with
>
>
>
> +x
>
>
That's a different conversion. Unary plus is ToNumber. Bitwise OR is ToInt32.
var x = undefined;
+x === 0; // false
(x|0) === 0; // true
It worked in that case because `ss` was undefined. But it was actually a typo.
>
> >> Using the binary logical operator for this purpose is a common ES idiom,
>
I agree. s||0 was what I wanted. I had a typo and it actually worked.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
ISO 8601 Code Review Wanted garrett.smith@acxiom.com - 2014-05-14 09:59 -0700
Re: ISO 8601 Code Review Wanted John Harris <niam@jghnorth.org.uk.invalid> - 2014-05-15 10:15 +0100
Re: ISO 8601 Code Review Wanted Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-15 21:31 +0200
Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-15 12:53 -0700
Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-15 13:12 -0700
Re: ISO 8601 Code Review Wanted Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-15 22:21 +0200
Re: ISO 8601 Code Review Wanted "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-05-16 00:44 +0200
Re: ISO 8601 Code Review Wanted Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-16 01:26 +0200
Re: ISO 8601 Code Review Wanted "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-05-16 09:39 +0200
bitwise vs. logical OR operator (was: ISO 8601 Code Review Wanted) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-16 12:50 +0200
Re: bitwise vs. logical OR operator (was: ISO 8601 Code Review Wanted) dhtml <dhtmlkitchen@gmail.com> - 2014-05-16 11:21 -0700
Re: ISO 8601 Code Review Wanted Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-16 22:26 +0100
Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-19 11:56 -0700
Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-19 17:03 -0700
Re: ISO 8601 Code Review Wanted Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-21 17:30 +0100
Re: ISO 8601 Code Review Wanted John Harris <niam@jghnorth.org.uk.invalid> - 2014-05-22 10:20 +0100
Re: ISO 8601 Code Review Wanted Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-23 20:44 +0100
Re: ISO 8601 Code Review Wanted John Harris <niam@jghnorth.org.uk.invalid> - 2014-05-24 15:54 +0100
Re: ISO 8601 Code Review Wanted Tim Streater <timstreater@greenbee.net> - 2014-05-22 18:46 +0100
Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-25 22:18 -0700
Re: ISO 8601 Code Review Wanted Tim Streater <timstreater@greenbee.net> - 2014-05-26 11:41 +0100
Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-28 09:54 -0700
Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-23 10:37 -0700
Re: ISO 8601 Code Review Wanted Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-24 19:09 +0100
Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-23 13:14 -0700
csiph-web